views:

269

answers:

4

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 select 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

A: 

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.

Josh Einstein
+1  A: 

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.

Reed Copsey
A: 

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();
}
viky
A: 

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

gkar