ContextMenu for DataGrid
I have the following context menu for rows of the Data Grid.
<ContextMenu x:Key="cm_rowMenu">
<!--ContextMenu For Row-->
<MenuItem Header="Edit Info."
Click="mnuEditInfo_Click"
/>
<MenuItem Header="Delete"
Click="mnuDeleteDevSoftware_Click"
/>
<MenuItem Header="Check In"
Click=开发者_运维百科"mnuCheckIn_Click"
/>
</ContextMenu>
<Style x:Key="DefaultRowStyle" TargetType="{x:Type dg:DataGridRow}">
<Setter Property="ContextMenu" Value="{StaticResource cm_rowMenu}" />
</Style>
However, I want to make the following change:
I want the menu items to be enabled/disabled based on properties of the dataGrid.SelectedItem. How do I do this ?
Best regards, MadSeb
Use commands:
<ContextMenu x:Key="cm_rowMenu" DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}">
<MenuItem Header="Edit Info." Command="{Binding EditCommand}"/>
</ContextMenu>
<Style x:Key="DefaultRowStyle" TargetType="{x:Type DataGridRow}">
<Setter Property="ContextMenu" Value="{StaticResource cm_rowMenu}" />
</Style>
Model for rows:
public class ItemModel
{
public ItemModel()
{
this.EditCommand = new SimpleCommand
{
ExecuteDelegate = _ => MessageBox.Show("Execute"),
CanExecuteDelegate = _ => this.Id == 1
};
}
public int Id { get; set; }
public string Title { get; set; }
public ICommand EditCommand { get; set; }
}
精彩评论