WPF text box and double click

I am showing the mac address in a WPF application. I want the MAC address to be optional to copy / paste, so I use ReadOnly TextBox

When the user double-clicks, I want to select the entire MacAddress

The default behavior in WPF and Windows is to double-click to select the part of the number between the colon so when the MAC address is: 00: 55: 66: 77: 99

and the user double-clicks, only one part of the MAC address will be selected (for example, 55) Is there a way without a code to make a choice for all the content for the text field

or maybe I shouldn't use a text box?

thanks

+4
source share
4 answers

On the MouseDoubleClick text box case, you can call the SelectAll () text box method to select Al text inside it.

void textBox_MouseDoubleClick(object sender, MouseButtonEventArgs e) { (sender as TextBox).SelectAll(); } 
+5
source

Unfortunately, I donโ€™t think there is a way to do this directly in a TextBox.

In this case, it would be trivial to add this behavior to the text field using the Attached Property or Expression Behavior (my preferences). Just watch the change of choice, and if there is something selected, select everything. Then you can easily use it in other places without adding code to your code for the files. You are still adding code, but not to the actual UserControl or Window class, but rather to the reusable component that will just be inserted into xaml.

+1
source

Can't you just handle the MouseDoubleClick event? Otherwise, if you want to always prevent partial selection, you can handle the SelectionChanged event. In any case, you can use the SelectAll method.

Nevermind I re-read and see that you want to use a non-codec solution. Unfortunately, I donโ€™t know anything.

0
source

I liked the idea of โ€‹โ€‹the behavior, but I had to redistribute some of the assemblies related to Blend, and I don't know Blend yet. So I create a new type of text field that inherits from the text field and makes selectAll when mousedoubleclick

Thanks for all the answers.

0
source

All Articles