Home > other >  Programmatically added Column to Datagrid keeps blue background style when selected
Programmatically added Column to Datagrid keeps blue background style when selected

Time:03-24

I have a Datagrid on my Window with 2 Columns added in XAML and 1 Column added in the code behind. I managed to remove the blue color from the first 2 columns by adding a DataGridCell Style with the background set to null. But in the code behind I cannot get it working.

This is the 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>

And this is my code behind:

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);
    }
}

I had hoped to change the background color by adding a Trigger to the style, but it doesn't work. What am I doing wrong?

CodePudding user response:

Found it! I was almost there with the Trigger. Not a DataTrigger, but a normal Trigger should be used. This is what the trigger should be:

        style.Triggers.Add(new Trigger
        {
            Property = DataGridCell.IsSelectedProperty,
            Value = true,
            Setters =
            {
                new Setter(BackgroundProperty, Brushes.Transparent),  // Or whatever color
                new Setter(BorderBrushProperty, Brushes.Transparent),
            }
        });
  • Related