I am using MVVM for a project and my question relates to using triggers.
The Items property of the TabControl has a property called items and items has an event called CurrentChanging. (TabControl.Items.CurrentChanging)
How do I wire up an event on the child of my main object using triggers?
Thanks
Edit:
This is using WPF and the MVVM-Light toolkit
This was solved by creating a Routed Event that was raised when the CurrentChangingEvent was fired in the code behind.
To make it fire I needed to add in the IsSynchronizedWithCurrentItem = true;
Related
I have a class which monitors a log file. It will fire an event when a new line is added.
What is the proper way, to update multiple controls in WPF?
Keep in mind that I am new to WPF bindings.
You should learn about bindings and MVVM. In MVVM you can have your viewmodel class implement INotifyPropertyChanged allowing the view to be automatically updated when a binding property in viewmodel class is updated. In your case your viewmodel can subscribe to the fired event and update a property which in turn will update the view (controls).
I have a derived Combobox control. In Autogeneratingcolumns event, I have assigned FrameworkElemnt of derived combobox as shown below:
var templateColumn = new DataGridTemplateColumn
{
CellTemplate = new DataTemplate
{
VisualTree = derivedComboFrameWorkElement
}
};
grid.Column = CreateTemplateColumn(templateColumn);
But, the SelectionChanged event does not fires for the combobox. The funny thing is that, once I inspect the visual tree (GridCell and my ComoboBox) using WPF-Inspector, the SelectionChanged event fires. So doubting that some issue with VisualTree updates. Please help me to get this working properly.
Regards,
ani
Good news. Issue has been identified. And the answer is, there was a PreviewMouseDown event and Focus() was called in the event. And thereby the dropdown was closed and was not available for click. I corrected the logic to solve the issue.
To identify this kind of issues, we can make use of WPF Inspector to check visual tree and Snoop which shows all events invoked. Snoop helped me to analyse the issue. Thanks for help.
Regards, ani
I have bound the WPF DataGrid to an observable collection of view models, where each view model represents each row in the DataGrid. The view model handles the BeginEdit and CellEditEnding events.
In one of the scenarios, I want to change the contents of the observable collection in the CellEditEnding event. But, I cant do this because the DataGrid is still in edit mode and if I try to add / remove items from the observable collection an exception is thrown and it causes my application to crash.
Any suggestions?
Dispatch it please.
In the CellEditEnding handler invoke your code with the Dispatcher.BeginInvoke() method.
Also you said
The view model handles the BeginEdit and CellEditEnding events.
If you are using pure MVVM then this is forbidden. MVVM implements events via Delegate/Relay Commands.
I've got a simple View with a single textbox that gets databound to a simple ViewModel with a single string property.
I need to catch the TextChanged event of that textbox so that I can do a little validation magic.
The problem that I am running into is that the TextChanged event fires for that textbox when the DataContext is set for the View.
Is there a standard mechanism that I can use to determine if the event is firing because of the DataContext being set versus when the user is making changes?
Thanks!
As far as I know there is no such mechanism. What you should do instead is to do your validation magic using standard means of WPF. Please see the following link: http://msdn.microsoft.com/en-us/library/ms752347.aspx#data_validation.
Anyway, as long as you use MVVM you can always detect that text has changed in the setter of the bound property in your view model.
I'm working in the MVVM design pattern with WPF. I have a ContextMenu with several items in it on a ListView. Based on the number of items selected in the ListView, I want to enable/disable certain MenuItems. Is there a way to route the SelectionChanged event along with the number of selected items in the ListView directly to the view model. If so, I can define a dependency property in the VM for IsEnabled quite easily. I'm just trying to avoid code-behind to handle this.
Kelly
You can use an attached behavior to route the SelectionChanged event to your VM. Basically, you create an attached property of type bool. When this property is set to true, you register an event handler for the SelectionChanged event of the target Menu.
Then an attached property can contains the command to execute (databound to a RelayCommand-like command in your VM).
Check those posts for more details:
http://www.japf.fr/2008/12/how-to-attach-commands-to-any-uielement/
http://marlongrech.wordpress.com/2008/12/13/attachedcommandbehavior-v2-aka-acb/