I use the context menu to change the rows DataGrid . Each menu item represents a color that is applied to the rows of the table. In the Colors collection, the colors themselves. The user can delete or add their own.
He brought a transparent color from the collection so that the user could not remove it and this item was always available.
The problem with linking the menu item "Without color". The command binds, but the command parameter that contains the rows of the table that need to be repainted is not attached. Although in the ItemContainerStyle the command parameter is bound in the same way and everything else works as it should. But with the point "Without color" this way, for some reason, does not work.
What is the problem?
<DataGrid.ContextMenu> <MenuItem Header="Цвет"> <MenuItem.ItemsSource> <CompositeCollection> <MenuItem Header="Без цвета" Command="{Binding ResultsVM.ResetColorCommand}" CommandParameter="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.SelectedItems}"/> <Separator /> <CollectionContainer Collection="{Binding Source={StaticResource Colors}}" /> <Separator /> <MenuItem Header="Редактировать метки" Click="miColorEdit_Click" /> </CompositeCollection> </MenuItem.ItemsSource> <MenuItem.ItemContainerStyle> <Style TargetType="{x:Type MenuItem}"> <Setter Property="Command" Value="{Binding ChangeColorCommand}" /> <Setter Property="CommandParameter" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}, Path=PlacementTarget.SelectedItems}" /> </Style> </MenuItem.ItemContainerStyle> </MenuItem> </DataGrid.ContextMenu> UPDATE
<DataGrid.ContextMenu> <ContextMenu x:Name="ContextMenu"> <MenuItem Header="Цвет"> <MenuItem.ItemsSource> <CompositeCollection> <MenuItem Header="Без цвета" Command="{Binding ResultsVM.ResetColorCommand}" CommandParameter="{Binding ElementName=ContextMenu, Path=PlacementTarget.SelectedItems}" /> <Separator /> <CollectionContainer Collection="{Binding Source={StaticResource Colors}}" /> <Separator /> <MenuItem Header="Редактировать метки" Click="miColorEdit_Click" /> </CompositeCollection> </MenuItem.ItemsSource> </MenuItem> </ContextMenu> <DataGrid.ContextMenu> UPDATE 2
ColorVM:
public class ColorVM : BaseVM { private string _title; private string _value; public string Title { get { return _title; } set { _title = value; OnPropertyChanged(); } } public string Value { get { return _value; } set { _value = value; OnPropertyChanged(); } } public ICommand ChangeColorCommand { get; set; } } Collection:
public static ObservableCollection<ColorVM> Colors { get; set; } = new ObservableCollection<ColorVM>() { new ColorVM {Title = "White", Value = "#ffffff"}, new ColorVM {Title = "Black", Value = "#000000"}, new ColorVM {Title = "Transparent", Value = "#00ffffff}, new ColorVM {Title = "Yellow", Value = "#ffff00"} }; xaml:
<CollectionViewSource Source="{Binding Colors}" x:Key="Colors"/>
ElementName. Maybe I'm doing something wrong? - trydex