WPF textbox not losing focus when selecting custom control - wpf

In a WPF/MVVM application I have a custom control on a particular view. This control extends WPF DataGrid and contains User names and ids.
On the same view I have some textboxes whose Text properties are bound to all different properties of User object exposed by the viewmodel and UpdateSourceTrigger for the Text properties are set to LostFocus.
Data gets updated as they should be whenever I leave a textbox (since the textbox looses focus). But problem is, this doesn't occur when I select any item in the custom control leaving any textbox, textbox data doesn't update. Can anyone explain what's happening?

Perhaps your custom control has its own focus scope defined, thus allowing logical focus to be in both the text box and your custom control? Try checking in snoop.

Related

Should a ViewModel control focus in the View

I have an ItemsControl binding to a collection in the ViewModel. As a result of user input, a new item gets added to the collection and this gets displayed on the View.
The item is also a View-ViewModel pair, the View contains a TextBox that I would like to receive focus immediately after being added to the collection.
How do I set the focus to a TextBox without referencing the View from within the ViewModel? Are attached properties the way to go here?
well you can do this by creating a behaviour..
have a look here to get an idea of behaviours controlling focus

Expand Usercontrol to full window size

I am currently working on a project which has a tab control which contains a Wrap panel which contain a series of user controls. I am looking for a way to allow the user to select one user control and maximize it to the size of the tab control/window.
One thought is to simply remove all the other items from the panel.However I am attempting to use MVVM as much as possible and I'm not sure how much the user control should know about the panel. (The user control will contain a button to allow maximizing)
Is there a way to temporarily remove the usercontrol from the grid and treat it like a modal popup or just to fill the window?
How about having "Visible" or "Maximized" bool properties in the view model for each user control based item, and databind said user controls Visibility property to the appropriate property. Then bind your user controls maximize/restore button to command in the view model to change the VM properties appropriately?

How to 'get at' the WPF combobox PART_EditableTextbox because combobox not getting highlighted?

My WPF combobox is populated with a different set of strings each click of a button. There are other controls on the window as well. The combobox is the 'first' (top) in the window, but the text doesn't get highlighted. When the user tabs through the controls, the text DOES get highlighted, but when it's the first on the window, it doesn't.
Maybe i need to force a highlight on the individual textbox control 'within' the combobox itself, but how would i do this? I couldnt seem to find the internal 'structure' of this control anywhere. Could anyone help here?
Jack
to get the TextBox of Combobox you can use
TextBox TxtBox = (TextBox)myCombo.Template.FindName("PART_EditableTextBox", myCombo);
I'm not sure it's the best solution, but you can use FrameworkElement.FindName to access the child control -- it's guaranteed to be present in a combobox, because it's a key constituent part of the control.
That stated, is it not better to try and call .Focus() on the control? That is likely why when you tab, the highlight is provided.
Another option is to derive from ComboBox, and expose the child text box as a property allowing you to set it's selection, or add a method directly to the combobox to set it for you.

WPF element binding across separate windows

I can do element-to-element binding in WPF: For example, I've got a window that has a slider control and a textbox, and the textbox dynamically displays the Value property of the slider as the user moves the slider.
But how do i do this across separate windows (in the same project, same namespace)?
The reason is that my main application window containing the textbox has a menu option that will open an 'options' window containing the slider control.
You should use a (global) ViewModel, containing the data you need to share, and bind to the property from that ViewModel.
This way the changes in either of windows are reflected in the bound data object, and back.
You dont. Point. Databinding has to go to an element accessible in the same control.
What you can do is have the options menu bind go an object that it has in it's own code (property) that gets populated to the same object the othe rwindow uses as data source.

WPF listview databinding - How do I force update to underlying object without tabbing out?

I have a WPF ListView that is bound to a BindingList<T>. The binding works like a charm, but I have to tab out of the cell to get the bound property to update....this is an issue because most users don't tab out of the last column before clicking the save button.
How do I force the list view to "persist" the changes to the bound DataContext without doing something hackish.
Bindings in WPF have a property called "UpdateSourceTrigger" which tells the Binding when to update the thing the UI is bound to. By default, it's set to "LostFocus" for the Text property which is what you're most likely using.
Change the trigger to "PropertyChanged" in your binding like this:
Text="{Binding Foo,UpdateSourceTrigger=PropertyChanged}"
... and now the source "Foo" property will be updated as the Text changes in the UI.
There's also an "Explicit" setting for UpdateSourceTrigger which is handy if you need to hold off writing any changes to the source until, say, the user clicks the OK button.

Resources