DataGrid 的上下文菜单

发布于 2024-09-28 08:31:56 字数 824 浏览 1 评论 0原文

我有以下数据网格行的上下文菜单。

        <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>

但是,我想进行以下更改:

我希望根据 dataGrid.SelectedItem 的属性启用/禁用菜单项。我该怎么做?

此致, 麦德塞布

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

盛夏尉蓝 2024-10-05 08:31:56

使用命令:

<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>

行模型:

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; }
}

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; }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文