How to add Command Binding to an Items control that does not have the command binding property
I use the MVVM pattern and I have the following code:
<DataTemplate x:Key="ActivityEditTemplate">
&l开发者_开发问答t;ItemsControl ItemsSource="{Binding Path=ActivityList}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<CheckBox Margin="2,0,2,0" Content="{Binding ActivityDescription}" IsChecked="{Binding IsSelected}" Command="vm:PatternViewModel.ActivityCommand" CommandParameter="{Binding ActivityTitle}">
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</DataTemplate>
The check box within the ItemsControl needs to allow for a multiselect. I have a routed command and can easily implement it on the checkbox, however I only want the command to execute when the items control is collapsed. The datatemplate is a static resource of an CellEditingTemplate in a WPF datagrid. I would like the code to still adopt the MVVM pattern. I considered the mouse leave event calling the command with no luck. Thanks in advance.
I am no WPF expert and I might read your question the wrong way.
However, I just read this:
There is an alternative way of associating a control in the view with a command object exposed by the view model. Only some controls can actually bind to a command through the Command property, notably those derived from System.Windows.Controls.Primitives.ButtonBase or System.Windows.Controls.MenuItem. If you want to attach a command to some other control or when you want to invoke a command on an event other than the click event for a button, you can use Expression Blend interaction triggers and the System.Windows.Interactivity.InvokeCommandAction class.
Source: blog.magnusmontin.net
You might wanna check out Event Triggers.
精彩评论