Ghassan Karwchan
Ghassan Karwchan

Reputation: 3539

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

Upvotes: 5

Views: 9707

Answers (4)

Ghassan Karwchan
Ghassan Karwchan

Reputation: 3539

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

Upvotes: 0

viky
viky

Reputation: 17669

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

Upvotes: 6

Reed Copsey
Reed Copsey

Reputation: 564491

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.

Upvotes: 1

Josh
Josh

Reputation: 69262

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.

Upvotes: 0

Related Questions