WPF textbox and doubleclick
I am displaying Mac Address in a WPF application. I want that mac address to be selectable to be copy/paste, so I am using ReadOnly TextBox
When the user double click I want to se开发者_开发知识库lect the whole MacAddress
The default behavior by the WPF and Windows, is by double click select part of the number between colons so when the mac address is : 00:55:66:77:99
and the user double click, only one part of the mac address (like 55) being selected Is there a way without a code to make the selection for the whole content for textbox
or maybe I should not use textbox?
Thanks
On MouseDoubleClick event of textbox you can call SelectAll() method of textbox to select al the text inside it.
void textBox_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
(sender as TextBox).SelectAll();
}
Unfortunately, I don't think there is a way to do this directly in a TextBox.
That being said, it would be trivial to add this behavior to a text box via an Attached Property or an Expression Behavior (my preference). Just watch for selection changed, and if there is anything selected, select everything. Then you could reuse this easily in other places, without adding code to your code behind files. You're still adding code, but not in the actual UserControl or Window class, but rather in a reusable component that will just be inserted into the xaml.
Can't you just handle the MouseDoubleClick event? Otherwise if you wanted to always prevent partial selection, you could handle the SelectionChanged event. In either case you can use the SelectAll method.
Nevermind I re-read and saw you want a non-code solution. Unfortunately I know of none.
I loved the idea of behavior, but I had to redistribute some Blend-related assembly, and I don't know Blend yet. So I end up creating a new type of textbox, that inherit from textbox, and does selectAll when mousedoubleclick
Thanks for all the answers
精彩评论