如何以编程方式添加DataTrigger时继承WPF DataGrid单元格样式?

时间:2018-03-13 14:00:09

标签: c# wpf material-design wpfdatagrid

我正在编写一个使用DataGrid控件的WPF应用程序。我使用MaterialDesign主题来设置应用程序的样式,这样可以提供漂亮的外观和感觉。

但是由于复杂的原因,我不会进入这里我需要以编程方式将列添加到dataGrid中。对于某些列,我还对列进行样式设置以突出显示红色的通过/失败。当我这样做时,我放松了一些造型'由该列的材料设计提供。即水平和垂直对齐。

DataGrid showing the third column alignment is not being picked up

以上代码如下:

            // Define Setter
            Setter setterResultFail = new Setter();
            setterResultFail.Property = DataGridCell.BackgroundProperty;
            setterResultFail.Value = Brushes.Red;

            // Create a column for the Site.
            var currentColumn = new DataGridTextColumn();
            currentColumn.Header = "Device #";
            currentColumn.Binding = new Binding("Device");
            ResultsDataGrid.Columns.Add(currentColumn);

            // Create a column for the Site.
            currentColumn = new DataGridTextColumn();
            currentColumn.Header = "Site";
            currentColumn.Binding = new Binding("Site");
            ResultsDataGrid.Columns.Add(currentColumn);

            // Create a column for the Pass Fail.
            currentColumn = new DataGridTextColumn();
            currentColumn.Header = "Pass Fail";
            currentColumn.Binding = new Binding("PassFail") { Converter = new BooleanToPassFailConverter() };

            // Create cellstyle to make the cell 'red' when the PassFail value is False. ( this is done via a data trigger )
            cellStyle = new Style(typeof(DataGridCell));

            // Define First DataTrigger that sets a CELL red if the value is a fail.
            dataTrigger = new DataTrigger();
            dataTrigger.Value = "False";
            dataTrigger.Binding = new Binding("PassFail");            
            dataTrigger.Setters.Add(setterResultFail);

            // Add the data-triggers to the cell style.
            cellStyle.Triggers.Clear();
            cellStyle.Triggers.Add(dataTrigger);

            // Apply the newly created cell style.
            currentColumn.CellStyle = cellStyle;

            ResultsDataGrid.Columns.Add(currentColumn);

显然,使用新的cellStyle而不是MaterialDesign样式。我已尝试手动设置垂直/水平值,但我无法让它看起来正确:

            Setter setterTextContentHorizonalAlignment = new Setter();
            setterTextContentHorizonalAlignment.Property = DataGridCell.HorizontalContentAlignmentProperty;
            setterTextContentHorizonalAlignment.Value = HorizontalAlignment.Center;

            Setter setterTextContentVerticalAlignment = new Setter();
            setterTextContentVerticalAlignment.Property = DataGridCell.VerticalContentAlignmentProperty;
            setterTextContentVerticalAlignment.Value = VerticalAlignment.Center;

            Setter setterTextHorizontalAlignment = new Setter();
            setterTextHorizontalAlignment.Property = DataGridCell.HorizontalAlignmentProperty;
            setterTextHorizontalAlignment.Value = HorizontalAlignment.Center;

            Setter setterTextVerticalAlignment = new Setter();
            setterTextVerticalAlignment.Property = DataGridCell.VerticalAlignmentProperty;
            setterTextVerticalAlignment.Value = VerticalAlignment.Center;

            cellStyle.Setters.Add(setterTextContentHorizonalAlignment);
            cellStyle.Setters.Add(setterTextContentVerticalAlignment);
            cellStyle.Setters.Add(setterTextHorizontalAlignment);
            cellStyle.Setters.Add(setterTextVerticalAlignment);

有没有办法可以添加到样式而不是替换它...类似于XAML中的BasedOn approch?

1 个答案:

答案 0 :(得分:0)

在这个问题上浪费了很多时间之后,我遇到了Danny Beckett的类似问题和King King的回答。通过使用他的答案并将其应用于特定单元格我遇到问题解决了问题:King King's answer

     // Create a column for the Pass Fail.
     currentColumn = new DataGridTextColumn();
     currentColumn.Header = "Pass Fail";
     currentColumn.Binding = new Binding("PassFail") { Converter = new BooleanToPassFailConverter() };

     // Create cellstyle to make the cell 'red' when the PassFail value is False. ( this is done via a data trigger )
     cellStyle = new Style(typeof(DataGridCell));

     // Define First DataTrigger that sets a CELL red if the value is a fail.
     dataTrigger = new DataTrigger();
     dataTrigger.Value = "False";
     dataTrigger.Binding = new Binding("PassFail");
     dataTrigger.Setters.Add(setterResultFail);

     // Add the data-triggers to the cell style.
     cellStyle.Triggers.Clear();
     cellStyle.Triggers.Add(dataTrigger);

     //root visual of the ControlTemplate for DataGridCell is a Border
     var border = new FrameworkElementFactory(typeof(Border));
     border.SetBinding(Border.BorderBrushProperty, new Binding("BorderBrush")
     {
           RelativeSource = RelativeSource.TemplatedParent
     });

     border.SetBinding(Border.BackgroundProperty, new Binding("Background") { RelativeSource = RelativeSource.TemplatedParent });
     border.SetBinding(Border.BorderThicknessProperty, new Binding("BorderThickness") { RelativeSource = RelativeSource.TemplatedParent });
     border.SetValue(SnapsToDevicePixelsProperty, true);

     //the only child visual of the border is the ContentPresenter
     var contentPresenter = new FrameworkElementFactory(typeof(ContentPresenter));
     contentPresenter.SetBinding(SnapsToDevicePixelsProperty, new Binding("SnapsToDevicePixelsProperty") { RelativeSource = RelativeSource.TemplatedParent });
     contentPresenter.SetBinding(VerticalAlignmentProperty, new Binding("VerticalContentAlignment") { RelativeSource = RelativeSource.TemplatedParent });
     contentPresenter.SetBinding(HorizontalAlignmentProperty, new Binding("HorizontalContentAlignment") { RelativeSource = RelativeSource.TemplatedParent });
     //add the child visual to the root visual
     border.AppendChild(contentPresenter);

     //here is the instance of ControlTemplate for DataGridCell
     var template = new ControlTemplate(typeof(DataGridCell));
     template.VisualTree = border;

     //define the style
     cellStyle.Setters.Add(new Setter(TemplateProperty, template));
     cellStyle.Setters.Add(new Setter(VerticalContentAlignmentProperty, VerticalAlignment.Center));
     cellStyle.Setters.Add(new Setter(HorizontalContentAlignmentProperty, HorizontalAlignment.Center));

     // Apply the newly created cell style.
     currentColumn.CellStyle = cellStyle;