【问题标题】:Remove the default style of selected row删除所选行的默认样式
【发布时间】:2014-11-04 13:29:17
【问题描述】:

我知道这个问题问了很多,但我只在 XAML 代码文件中看到它。
我正在处理数据网格扩展,所以我在 C# 代码文件中,我想知道如何删除选定行的默认样式(在我的情况下,我不想改变样式,我在行标题中有一个图像显示选择了哪一行)。

附带问题,我们能否在 C# 代码中按下“Ctrl”之类的选择?

谢谢

编辑

我试试这个代码:

Style oRow = new Style(typeof(DataGridRow));
DataTrigger oTrigger2 = new DataTrigger();
Binding oBind = new Binding();
oBind.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(DataGridRow), 1);
oBind.Path = new PropertyPath(DataGridRow.IsSelectedProperty);
oTrigger2.Binding = oBind;
oTrigger2.Value = true;
oTrigger2.Setters.Add(new Setter(DataGridRow.BackgroundProperty, Brushes.Khaki));
oRow.Triggers.Add(oTrigger2);
this.RowStyle = oRow;

目前,我尝试将选定的背景放在卡其色中进行测试。但我得到了旧的蓝色高光。

编辑 2

基于Sinatr的思路,我将DatagridRow改为DatagridCell,以:

Style oRow = new Style(typeof(DataGridCell));
DataTrigger oTrigger2 = new DataTrigger();
Binding oBind = new Binding();
oBind.RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(DataGridRow), 1);
oBind.Path = new PropertyPath(DataGridRow.IsSelectedProperty);
oTrigger2.Binding = oBind;
oTrigger2.Value = true;
oTrigger2.Setters.Add(new Setter(DataGridCell.BackgroundProperty, null));
oTrigger2.Setters.Add(new Setter(DataGridCell.BorderBrushProperty, null));
oRow.Triggers.Add(oTrigger2);
this.RowStyle = oRow;

我只需要获取行的前景来设置单元格的前景。但是我对该解决方案提出了一个新问题,可以将背景设置为 null 还是我应该将其绑定到 thr 行背景?

【问题讨论】:

  • 我认为你必须重新设计DataGrid.RowStyle。只需不要为Selector.IsSelected 添加任何触发器,那么所有行的外观都应该相同。
  • 我在编辑之前尝试过,但它不起作用。这就是为什么我试图改变卡其色的原因

标签: c# wpf datagrid


【解决方案1】:

你必须替换CellStyle控制模板。

例如,这个愚蠢的模板

<DataGrid x:Name="dataGrid">
    <DataGrid.CellStyle>
        <Style TargetType="DataGridCell">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="DataGridCell">
                        <TextBlock Text="1" Background="Khaki" Foreground="Red"/>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </DataGrid.CellStyle>
</DataGrid>

将产生此输出(仅适用于 1 个Tuple&lt;string, string, string&gt;,无论如何):

选择或未选择的项目保持不变。

显然你必须更正确地实现它,但给出的答案应该会给你一个想法。

【讨论】:

  • 谢谢,根据您的解决方案,我编辑了我的问题。我没有标记为已解决,因为我有另一个关于我的解决方案的问题。希望有人能回答我。
  • 有人会帮忙的。
猜你喜欢
  • 2014-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-08-25
  • 2012-08-31
  • 2016-08-05
相关资源
最近更新 更多