以编程方式将 Column 添加到 Datagrid,在选择时保持蓝色背景样式
Programmatically added Column to Datagrid keeps blue background style when selected
我的 Window 上有一个数据网格,在 XAML 中添加了 2 列,在后面的代码中添加了 1 列。通过添加背景设置为 null 的 DataGridCell 样式,我设法从前 2 列中删除了蓝色。
但是在后面的代码中我无法让它工作。
这是Window.xaml
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:System="clr-namespace:System;assembly=mscorlib" x:Class="WpfApp1.MainWindow"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Background" Value="{x:Null}" />
<Setter Property="BorderBrush" Value="{x:Null}" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="{x:Null}" />
<Setter Property="BorderBrush" Value="{x:Null}" />
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<DataGrid x:Name="CandidatesEpisodesMatrix"
ColumnWidth="*"
AutoGenerateColumns="False" CanUserAddRows="False" HeadersVisibility="Column" SnapsToDevicePixels="True" SelectionUnit="Cell" RowDetailsVisibilityMode="Collapsed" >
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name}" />
<DataGridCheckBoxColumn/>
<!-- Other columns are added dynamically -->
</DataGrid.Columns>
<System:Object/>
<System:Object/>
</DataGrid>
</Grid>
</Window>
这是我背后的代码:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
AddMatrixColumn();
}
private void AddMatrixColumn()
{
var factory = new FrameworkElementFactory(typeof(CheckBox));
var columnCellTemplate = new DataTemplate(typeof(CheckBox));
columnCellTemplate.VisualTree = factory;
var style = new Style();
style.Setters.Add(new Setter(HorizontalAlignmentProperty, HorizontalAlignment.Stretch));
style.Triggers.Add(new DataTrigger
{
Binding = new Binding("IsSelected"),
Value = true,
Setters =
{
new Setter(BackgroundProperty, Brushes.BlueViolet),
new Setter(BorderBrushProperty, Brushes.Aqua),
}
});
var headerStyle = new Style();
headerStyle.Setters.Add(new Setter(HorizontalAlignmentProperty, HorizontalAlignment.Center));
var column = new DataGridTemplateColumn();
column.Header = "episode.Name";
column.HeaderStyle = headerStyle;
column.CellTemplate = columnCellTemplate;
column.CellStyle = style;
CandidatesEpisodesMatrix.Columns.Add(column);
}
}
我曾希望通过在样式中添加触发器来更改背景颜色,但没有成功。我做错了什么?
找到了!我几乎和扳机在一起了。不是 DataTrigger
,而是正常的 Trigger
。触发器应该是这样的:
style.Triggers.Add(new Trigger
{
Property = DataGridCell.IsSelectedProperty,
Value = true,
Setters =
{
new Setter(BackgroundProperty, Brushes.Transparent), // Or whatever color
new Setter(BorderBrushProperty, Brushes.Transparent),
}
});
我的 Window 上有一个数据网格,在 XAML 中添加了 2 列,在后面的代码中添加了 1 列。通过添加背景设置为 null 的 DataGridCell 样式,我设法从前 2 列中删除了蓝色。 但是在后面的代码中我无法让它工作。
这是Window.xaml
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:System="clr-namespace:System;assembly=mscorlib" x:Class="WpfApp1.MainWindow"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Background" Value="{x:Null}" />
<Setter Property="BorderBrush" Value="{x:Null}" />
<Style.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter Property="Background" Value="{x:Null}" />
<Setter Property="BorderBrush" Value="{x:Null}" />
</Trigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid>
<DataGrid x:Name="CandidatesEpisodesMatrix"
ColumnWidth="*"
AutoGenerateColumns="False" CanUserAddRows="False" HeadersVisibility="Column" SnapsToDevicePixels="True" SelectionUnit="Cell" RowDetailsVisibilityMode="Collapsed" >
<DataGrid.Columns>
<DataGridTextColumn Binding="{Binding Name}" />
<DataGridCheckBoxColumn/>
<!-- Other columns are added dynamically -->
</DataGrid.Columns>
<System:Object/>
<System:Object/>
</DataGrid>
</Grid>
</Window>
这是我背后的代码:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
AddMatrixColumn();
}
private void AddMatrixColumn()
{
var factory = new FrameworkElementFactory(typeof(CheckBox));
var columnCellTemplate = new DataTemplate(typeof(CheckBox));
columnCellTemplate.VisualTree = factory;
var style = new Style();
style.Setters.Add(new Setter(HorizontalAlignmentProperty, HorizontalAlignment.Stretch));
style.Triggers.Add(new DataTrigger
{
Binding = new Binding("IsSelected"),
Value = true,
Setters =
{
new Setter(BackgroundProperty, Brushes.BlueViolet),
new Setter(BorderBrushProperty, Brushes.Aqua),
}
});
var headerStyle = new Style();
headerStyle.Setters.Add(new Setter(HorizontalAlignmentProperty, HorizontalAlignment.Center));
var column = new DataGridTemplateColumn();
column.Header = "episode.Name";
column.HeaderStyle = headerStyle;
column.CellTemplate = columnCellTemplate;
column.CellStyle = style;
CandidatesEpisodesMatrix.Columns.Add(column);
}
}
我曾希望通过在样式中添加触发器来更改背景颜色,但没有成功。我做错了什么?
找到了!我几乎和扳机在一起了。不是 DataTrigger
,而是正常的 Trigger
。触发器应该是这样的:
style.Triggers.Add(new Trigger
{
Property = DataGridCell.IsSelectedProperty,
Value = true,
Setters =
{
new Setter(BackgroundProperty, Brushes.Transparent), // Or whatever color
new Setter(BorderBrushProperty, Brushes.Transparent),
}
});