【问题标题】:WPF DataGrid Nested Bands - Remove previous selected row colorWPF DataGrid Nested Bands - 删除先前选定的行颜色
【发布时间】:2014-08-15 21:17:00
【问题描述】:

我有一个带状 WPF DataGrid,如果您选择顶部带,该行将变为蓝色以表示它是 selectedRow(默认情况下)。但是,当用户随后从第二个波段中选择一行时,我希望顶部波段的蓝色消失,因为这意味着 很难发现您在层次结构中进一步选择了哪一行

这可以通过 XAML 或 C# 实现吗?

通过 XAML,我可以在每个波段上使用类似的东西 (RowDetailsTemplate)

 <DataGrid.RowStyle>
     <Style TargetType="DataGridRow">
        <Style.Triggers>
           <Trigger Property="IsSelected"
                    Value="True">
              <Setter Property="BorderBrush"
                    Value="Blue" />
              <Setter Property="BorderThickness"
                    Value="2" />
           </Trigger>
        </Style.Triggers>
     </Style>
  </DataGrid.RowStyle>

通过 c#,当他们点击一行时,我可以得到 selectedRow

DataGridRow selectedRow = DataGridRow.GetRowContainingElement(expandCollapseButton);

所以我有几种可能性,但我需要帮助才能获得“上一行然后将其取消着色”

干杯

【问题讨论】:

  • 您能否分享一些当前和预期输出的屏幕?

标签: c# wpf xaml datagrid


【解决方案1】:

如果我对您的理解正确,您只想取消选择DataGrid 中的选定行,当一行被选中时从第二个乐队(无论是什么)。从DataGrid 中删除选择的一种简单方法是将SelectedItem 属性设置为null

<DataGrid Name="DataGrid" ItemsSource="{Binding Items}" ... />

Then in code behind, when the other row is selected (possibly in a SelectionChanged event handler), you can simply call this:

private void SecondBandSelectionChanged(object sender, SelectionChangedEventArgs e)
{
    DataGrid.SelectedItem = null;
}

【讨论】:

  • 干杯是有道理的,第二个波段只是该行的 DataGrid.RowsDetailTemplate。乐队是它的基因词,尤其是在基础设施库中
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-17
  • 1970-01-01
  • 2012-06-04
  • 1970-01-01
  • 2019-06-22
  • 2023-03-24
相关资源
最近更新 更多