【问题标题】:DevExpress GridControl ComboBoxEdit list item context menu command bindingDevExpress GridControl ComboBoxEdit 列表项上下文菜单命令绑定
【发布时间】:2018-06-26 14:51:38
【问题描述】:

我有这个使用 CellTemplateSelector 动态创建的 DevExpress GridControl。 GridControl 列之一由如下所示的 DataTemplate 定义:

        <DataTemplate x:Key="NameComboColumnTemplate">
        <ContentControl>
            <dxg:GridColumn
                x:Name="GridColumnName"
                FieldName="{Binding Path=(dxci:DependencyObjectExtensions.DataContext).FieldName, RelativeSource={RelativeSource Self}}"
                Header="{Binding Path=(dxci:DependencyObjectExtensions.DataContext).Header, RelativeSource={RelativeSource Self}}" 
                Width="{Binding Path=(dxci:DependencyObjectExtensions.DataContext).Width, RelativeSource={RelativeSource Self}}">
                <dxg:GridColumn.CellTemplate>
                    <DataTemplate>
                        <dxe:ComboBoxEdit
                            IsTextEditable="False"
                            SelectedItem="{Binding RowData.Row.SelectedCylinderName, Mode=TwoWay}"
                            ItemsSource="{Binding RowData.Row.NameList, Mode=TwoWay}">
                            <dxe:ComboBoxEdit.ItemContainerStyle>
                                <Style TargetType="dxe:ComboBoxEditItem">
                                    <Setter Property="dxb:BarManager.DXContextMenu">
                                        <Setter.Value>
                                            <dxb:PopupMenu>
                                                <dxb:BarButtonItem 
                                                    x:Name="BarButtonItemName"
                                                    Content="Delete"
                                                    Command="{Binding DeleteNameCommand}"
                                                    CommandParameter="{Binding}"/>
                                            </dxb:PopupMenu>
                                        </Setter.Value>
                                    </Setter>
                                </Style>
                            </dxe:ComboBoxEdit.ItemContainerStyle>
                        </dxe:ComboBoxEdit>
                    </DataTemplate>
                </dxg:GridColumn.CellTemplate>
            </dxg:GridColumn>
        </ContentControl>
    </DataTemplate>

存在一个名为 DataGridRow 的类,它包含 GridControl 数据的一行的属性。该类还包含一个定义为

的命令
    public ICommand DeleteNameCommand => new DelegateCommand<object>(obj => DeleteName(obj));

    private void DeleteName(object obj)
    {
        // the obj parametercontains the text present on the ComboBoxEdit list item that was
        //  right-clicked to display the context menu.

        // Delete the name from the list here
    }

如上所示,ComboBoxEdit SelectedItem 和 ItemsSource 属性绑定到通过 RowData.Row 属性访问的 DataGridRow 属性, strong>DeleteNameCommand 也可以通过 RowData.Row 属性访问。

当用户单击 ComboBoxEdit 向下箭头时,会显示名称列表,当用户右键单击列表名称时,会显示上下文菜单。由于 PopupMenu/BarButtonItem 不是可视树的一部分,我如何将 BarButtonItem Command 属性绑定到在 ComboBoxEdit 中访问的 RowData.Row 属性? ...以及如何将右键单击的 ComboBoxEdit 列表项的文本作为 CommandParameter 的值传递?

非常感谢任何指向正确方向的指针。

【问题讨论】:

    标签: binding contextmenu devexpress-wpf


    【解决方案1】:

    好的……经过几天的尝试,DevExpress 的优秀人员能够为我提供解决方案。我很抱歉发布这个问题,因为我没想到(我的错)会这么快得到解决方案(我昨天下午问过他们),但我认为将解决方案告知社区很有用。

    可以通过

    访问 ComboBoxEdit 编辑器数据上下文

    (dxe:BaseEdit.OwnerEdit).DataContext

    并访问相关的 RowData.Row 属性,该属性本身包含可用于每一行 column 的属性,可通过

    访问

    (dxe:BaseEdit.OwnerEdit).DataContext.RowData.Row

    因此,PopupMenu/BarButtonItem Command 可以绑定到 DataGridRow 类中包含的 DeleteNameCommand,该类本身可以通过 RowData.Row 通过指定以下内容进行访问:

    Command="{Binding Path=(dxe:BaseEdit.OwnerEdit).DataContext.RowData.Row.DeleteNameCommand,RelativeSource={RelativeSource Self}}"

    ...并且右键单击的 ComboBoxEdit 列表项的文本可通过以下声明作为 CommandParameter 的值传递:

    CommandParameter="{Binding}"

    ... 非常高兴:-)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-04
      • 1970-01-01
      • 2019-04-14
      • 1970-01-01
      • 2011-04-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多