Controls in a WPF UserControl don`t raise lost focus - wpf

I have a MainWindow with 3 main buttons at the top and below a MainUserControl.
In the MainUserControl I have at top 3 UserControls with ButtonBars
and at the bottom a DataGrid.
When I enter data in a DataGridCell and I click into another cell a property change is fired in my ViewModel bound to the DataGrid.
When I enter data... and I click on one of the 3 main buttons again a property change is fired because of Lost Focus event.
When I ... and I click on one of the buttons in the ButtonBar in the UserControl no property change is fired because there seem to be no Lost Focus event.
How can I fix that?

FocusManager.IsFocusScope="False" on the UserControl or other elements like Menu solved the problem and my property changes are raised now in the model :)

If you change your binding to to set UpdateSourceTrigger=PropertyChanged then you will not need to rely on the LostFocus to do a property update.

Related

WPF UserControl OnGotFocus/OnLostFocus

I've created a WPF UserControl with some TextBoxes, but if I click into a TextBox on it, the OnGotFocus of the UserControl is not called.
How can I get it? Or what is the best practice in that situation?
The OnGotFocus method is fired in response to the GotFocus routed event that have the bubbling routing strategy. This method is not fired because the corresponding event is processed at the embedded child control level.
You can check GotFocus/LostFocus events. These event should be fired for your TextBox and UserControl as well.

CheckBox UserControl Always Checked

I have a usercontrol called RateView.xaml.cs. This user control contains a checkbox called CheckBox1.
I put this user control on my MainWindow 5 times. I need three of the usercontrol's on the MainWindow to have the checkbox on from the start of the app. How can this be accomplished?
Thanks
Create a dependency property IsChecked on the UserControl, bind it to the internal CheckBox, set that property accordingly on the instances of the UserControl.

How to pass selected radio button in the next window and show that as selected in WPF?

I have a WPF Window with a datagrid dgSample. it has been bound to a list lstSample like this:
dgSample.itemssource=lstSample;
this datagrid also has a radio button column wherein i select one row by clicking on the radio button, and then, i can move to the next page after i click on the next button. On the next page, there is again the same datagrid, with the same radiobutton column. What I want is, that when i reach this page, i want the radio button that was selected in the previous page to be selected here as well.
I have tried binding the radiobutton column with an IsSelected Property by doing:
IsChecked="{Binding Path IsSelected, Mode=TwoWay}"
but this is not working.
What can I do to make it work?
P.S.: I prefer code-behind solution than the xaml one.
Please help !
Your model needs to implement INotifyPropertyChanged and call
PropertyChanged(this, new PropertyChangedEventArgs("IsSelected"))
to get it to update in another view.
NB: If you set
public event PropertyChangedEventHandler PropertyChanged = delegate { };
you won't have to check for null.

How can I set the keyboard focus to the content of a ContentPresenter when it changes?

I have a ContentControl in my view which is databound to the CurrentItem property of my viewmodel. The objects that are exposed via CurrentItem each has its own DataTemplate.
When the CurrentItem property changes, the appropriate DataTemplate for that item is displayed, as it should be. However, I cannot find out a way to set the keyboard focus to the content of the DataTemplate.
Even if I manually set the keyboard focus to the DataTemplate, if the CurrentItem property changes (and a new Template is instantiated) the focus is lost (FocusManager.GetFocussedElement returns null).
How can I set the keyboard focus to the content of the ContentPresenter when it changes?
I believe you can use the LayoutUpdated event on either your ContentControl or the ContentPresenter. This should fire anytime the Content/ContentTemplate/etc changes.
Alternatively, you could derive a class from ContentControl then override the OnContentChanged, OnContentTemplateChanged, etc methods. Then you'd need to search down the visual tree and set the focus. You may need to use the Dispatcher to delay the focus setting code.

WPF: Detect when selected item changes

I have a control that is data-bound to a ListBox. All of the bound properties are being updated correctly. However, the control needs to know when the selected item changes so that it can do some other cleanup. Is there an event that covers this?
You can also bind to the SelectedItem property, say with ICollectionView.CurrentItem, and set the IsSynchronizedWithCurrentItem property to True.
There is SelectionChanged event in ListBox.

Resources