WPF textbox and doubleclick - wpf

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

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

Related

Winforms Databinding and multiple Forms

i have a main window that contains multiple UserControls, arranged as tab pages and tab groups (much like Visual Studio allows to have two or more editors visible at the same time).
I also have the possibility to open such an UserControl into a seperate floating window.
One of these UserControls contains simple form fields (e.g. text boxes). These text boxes are bounded with common databinding to an object / property. The binding mode is OnValidation (not on OnPropertyChanged).
When I switch the focus from this User Control inside the main window into another UserControl in the Main windows, the validation is automatically performed and the databinding is finised / the changed text will be set on the model object / property that is bounded to that text field.
But if I switch the focus to an UserControl which resides in another (floating) window, the databinding is not finished since no validation is performed.
I know that I can handle this manually by triggering ValidateChildren etc, but this seems to my the wrong way / is ugly.
Is there a "correct" / clean way to solve this issue? I want that the validation is performed as soon as the UserControl loses its focus or the window gets deactivated.
One information: On of my UserControls contains a TreeControl. If I edit a tree node label, and when I switch the focus to another (foating) window, the label edit is finished automatically. I want the corresponding behaviour for usual form fields regarding binding...
Thanks for help!
There is no automatic way to do this. From the point of view of the control, it still has the focus (if you click the title bar or Alt-tab back to the main window, you will notice that the focus remains in the same control). Its just that the form the control is on is not active. If you want it to save changes when your form is deactivated, you must manually trigger it. The best way to do that is probably to override the OnDeactivate method of the form.
protected override void OnDeactivate(EventArgs e)
{
base.OnDeactivate(e);
this.ValidateChildren();
}

Deriving UIElement - how to receive focus

I created a class based on UIElement and my intention is to render it myself overriding OnRender. Rendering works fine. Next I want to implement focus management and continue with other aspects of LIFE, but overriding GotFocus and calling Me.Focus() in it don't do a single thing. I places my control on a Window with one another control - TextBox, and clicking on it doesn't do a single think. Tab doesn't set focus too, and TextBox is AcceptsTab negative. I know I will have to visualize focus somehow in OnRender to actually tell when the control is focused or not, but first I need to allow it to receive focus and that's where I struggle. Could you please help me out?
P.S. I tagged this with FrameworkElement because I don't have enough reputation to create a tag UIElement and leaving tags empty seemed like a silly thing to do.
Converting my comment into an answer:
I think you'd probably be better off deriving from FrameworkElement instead.

wpf selecting controls inside a listbox

I have a textbox and some labels inside the data template of bounded listbox.
When I click on any label the whole item is highlighted in blue, but when I click directly on a different textbox the selection does not change.
Is there a way to make the selection of the listbox change even when a textbox is clicked?
thanks
This is what I've exactly asked few days ago, see post: "WPF: Trigger SelectedIndex changed whilst clicking on any control within a ListBoxItem area"
basically there are few solutions, using code behind and XAML, but I've not verified latter approach yet
The reason is because the TextBox handles the click event in order to receive focus. There are a number of ways to handle this, including but not limited to:
stop the TextBox handling mouse events (which prevents the user from focussing it using the mouse)
use an eventhandler when the TextBox gains focus (or PreviewClick or similar), to select the parent ListItem

Silverlight 4 DataGrid validation info

I have an ObservableCollection where T: INotifyDataErrorInfo. If I use the DataGrid in edit mode, and there is an error on the object, beside the exact column displaying a red border, the BACKGROUND of the ENTIRE ROW goes red.
I want the same functionality, but in readonly mode as well! The problem is, the DataGrid doesn't listen for ErrorsChanged event unless it goes into edit mode. How can I fix it? Do I have to subclass DataGrid? (I'd prefer not to, I'd rather use Styles or Behaviors) Where can I access the style of the row's background?
EDIT:
I've tried two new things:
I've subscribed to the datagrid's RowLoading event, and subscribed to the Row's Loaded and the DataContext's ErrorsChanged event from there (and unsubscribed on RowUnloading), and tried setting the VisualState of the row from these event handlers based on the validity of the DataContext. However, since setting the visual state doesn't change the actual state of the control so when it has to change states (for example on mouseover or selection) it changes back to Valid.
Using the trick I've learned from ComboBoxes (see here), I've tried to bind the DataContext to the DataGridRow.Tag, using row.SetBinding(DataGridRow.TagProperty,new Binding()); (since the Source is the DataContext and the path is the entire object), but absolutely nothing happened, like contrary to my earlier findings with ComboBox controls doesn't necessarily listen for Validation on Every DependencyProperty.
So it's getting less likely that there is a practical solution. Any thoughts?
Have a look at the following question, it might help
C# Silverlight Datagrid - Row Color Change

WPF ListBox as RadioButtonList - mimic SelectionChanging Event via WPF validation

I am using ListBox for having RadioButtonList behaviour (that's what people recommend as there is no inherent radio button list in WPF). The listbox is bound to a ViewModel.
Now, whenever user changes the selection on listbox, I want to check whether user has some unsaved data on the part of screen and prompt accordingly (typical yes,no,cancel). If I use SelectionChanged event, the selection has already occured and hence, prompting is of no use. And there doesn't seem to be any SelectionChanging event.
I am am not sure but can I mimic SelectionChanging behaviour by using WPF binding validation rules?
Or should I use MouseButtonDown event? Would that lead to problems?
You don't need to put them in a listbox, the are group-able like so:
<RadioButton GroupName=“One“ IsChecked=“True“/>
Notice the GroupName, this holds the collection together, as for the rest of your question I am having a slight issue understanding what you need, sorry :(

Resources