i'm trying to understand how Routed Commands works, but I've got a problem. I've created a Main Window with Button and ItemControl with UserControls as its Item template.
<Window>
<Grid>
<ItemsControl
ItemsSource="{Binding CollectionOfUsers}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<uc:UserUserControl
Name="{Binding PersonName}"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<StackPanel />
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
</ItemsControl>
<Button
Command="{x:Static helpers:RoutedCommands.SendChangesCommand}"
Content="SAVE"/>
</Grid>
</Window>
If button in Main Window is Clicked I want to run some method from every UserControl in ItemsControl.
I've created RoutedCommand in static class:
public static class RoutedCommands
{
public static readonly RoutedCommand SendChangesCommand = new RoutedCommand();
}
And bound UserControl to RoutedCommand.
<UserControl.CommandBindings>
<CommandBinding Command="{x:Static helpers:RoutedCommands.SendChangesCommand}"
Executed="CommandBinding_Executed"/>
With method in code-behind:
private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
// Do something
}
Thought It will fire method on every User Control object when I click the button, but sadly this code doesn't work - button is disabled. What am I missing?