【问题标题】:WPF GridControl: prevent row selection in DataControlDetailDescriptor GridControl or select master on detail selectionWPF GridControl:防止在 DataControlDetailDescriptor GridControl 中选择行或在详细信息选择时选择主控
【发布时间】:2014-09-24 05:44:25
【问题描述】:

我还没有找到任何令人满意的答案或解决方案来解决我的问题。我有一个主-细节网格(仅 2 个级别),仅单选。一些相关代码:

<dxg:GridControl AutoGenerateColumns="None" Name="gridControlMaster" AutoExpandAllGroups="True">
    <dxg:GridControl.View>
        <dxg:TableView ShowGroupPanel="False" />
    </dxg:GridControl.View>
    <dxg:GridControl.Columns>
        <dxg:GridColumn FieldName="Entry.Date" Header="Entry date" Visible="True" AllowEditing="False" />
    </dxg:GridControl.Columns>
    <dxg:GridControl.DetailDescriptor>
        <dxg:DataControlDetailDescriptor ItemsSourcePath="EntrySubList" ShowHeader="False">
            <dxg:DataControlDetailDescriptor.DataControl>
                <dxg:GridControl AutoGenerateColumns="None" AutoExpandAllGroups="True">
                    <dxg:GridControl.View>
                            <dxg:TableView AutoWidth="True" ShowGroupPanel="False" ShowColumnHeaders="False" />
                        </dxg:GridControl.View>
                    <dxg:GridControl.Columns>
                        <dxg:GridColumn FieldName="Label" Header="Label" Visible="True" AllowEditing="False" />
                        <dxg:GridColumn FieldName="Value" Header="Value" Visible="True" AllowEditing="False" />
                    </dxg:GridControl.Columns>
                </dxg:GridControl>
            </dxg:DataControlDetailDescriptor.DataControl>
        </dxg:DataControlDetailDescriptor>
    </dxg:GridControl.DetailDescriptor>
</dxg:GridControl>

在某些时候(比如在 GridControl 之外单击某个按钮)我需要主网格的当前选定项,而不管详细网格中选定的子项如何!当前,如果我单击主网格中的某行,然后单击详细网格中的某行(与主网格中先前选择的行不同的父级),则 CurrentItem 或 SelectedItem 不是所需的(它是主网格,当它至少应该是当前突出显示的细节项的父级时)。这可能会误导用户。

理想的解决方案是拥有某种“选择单元”,在我的情况下是整个主项目,以及所有子项目(详细网格)。因此,无论用户单击网格中的哪个项目(主项目或详细信息),都会选择主项目及其所有子项目(突出显示)。主网格的 CurrentItem 是当前选中的父项。

另一种可能的解决方案是只允许选择主网格项目 - 并防止在详细网格中选择(仅在详细 GridControl 上设置 SelectionMode = none 不会这样做!)。在这种情况下,另一个不错的功能是单击子项(试图选择它)会在主网格中选择其父项。

如果需要任何进一步的信息,我会提供。

谢谢你的回答。

【问题讨论】:

    标签: c# wpf devexpress gridcontrol


    【解决方案1】:

    据我所知,您描述的问题已经在 DevExpress 支持中心的上下文中讨论过 Master-detail grid SelectedItem behaves unexpectedly when clicking detail rows 线程。 此线程中提供的解决方案 - 创建与主视图的选定项关联的附加属性,并在 FocusedViewChanged 事件处理程序中更新此属性。请看一下上述讨论所附的sample project。我相信它应该把你推向正确的方向。

    【讨论】:

      【解决方案2】:

      谢谢你的指点,它真的帮助了我。我之前已经尝试过使用 FocusedView 属性,但显然不是正确的方法(可能在错误的网格级别上)。您的回答和链接帮助我解决了我的问题。

      我做了这样的事情:

      <dxg:GridControl AutoGenerateColumns="None" Margin="0,36,1,0" Name="gridControlMaster" AutoExpandAllGroups="True">
                                                  <dxg:GridControl.View>
                                                      <dxg:TableView ShowGroupPanel="False" FocusedViewChanged="gridControlMaster_TableView_FocusedViewChanged" />
      

      void gridControlMaster_TableView_FocusedViewChanged(object sender, FocusedViewChangedEventArgs e)
      {
                  int rowHandle = ((GridControl)e.NewView.DataControl).GetMasterRowHandle();
                  if (GridControl.InvalidRowHandle == rowHandle) return;                            
      
                  gridControlMaster.SelectedItem = gridControlMaster.GetRow(rowHandle);
      }
      

      绑定到主 GridControl 的 SelectedItem 属性的一些测试文本框显示在 Detail GridControl 中选择子项时选择了正确的主记录。这对我来说已经足够了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-10-27
        • 1970-01-01
        • 2011-01-28
        • 2012-03-29
        • 2016-03-24
        • 2014-07-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多