简体   繁体   English

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

[英]How do I Inherit a WPF DataGrid Cell Style when programmatically adding a DataTrigger?

I'm writing a WPF application which makes use of the DataGrid control. 我正在编写一个利用DataGrid控件的WPF应用程序。 I'm using the MaterialDesign theme to style the application and this gives a nice look and feel. 我正在使用MaterialDesign主题来设置应用程序的样式,这将给用户带来很好的外观。

However for complex reasons I wont go into here I'm required to add the columns into the dataGrid programmatically. 但是,由于复杂的原因,我不会在这里进行介绍,我需要以编程方式将这些列添加到dataGrid中。 For some of the columns I'm also styling the columns to highlight pass / fail in red. 对于某些列,我还对列进行样式设置,以红色突出显示通过/失败。 When I do this I loose 'some of the styling' provided by material design for that columns. 当我这样做时,我会松开该列的材料设计所提供的“某些样式”。 Namely the Horizontal and Vertical alignment. 即水平和垂直对齐。

显示第三列对齐方式的DataGrid未被拾取

The code to the above is as follows: 上面的代码如下:

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

Clearly the new cellStyle is used instead of the MaterialDesign style. 显然,将使用新的cellStyle代替MaterialDesign样式。 I've tried setting the values for vertical / horizontal manually but I can't get it to look correct: 我尝试过手动设置垂直/水平的值,但无法正确显示:

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

Is there a way I can add to the style rather than replace it...similar to the BasedOn approch in XAML? 有没有一种我可以添加该样式而不是替换它的方法...类似于XAML中的BasedOn方法?

After much wasting of time on this question I came across Danny Beckett's similar question and King King's answer. 在这个问题上浪费了很多时间之后,我遇到了丹尼·贝克特(Danny Beckett)的类似问题和金国王(King King)的回答。 By using his answer and applying it to the specific cell I was having trouble with it fixed the issue: King King's answer 通过使用他的答案并将其应用于特定的单元格,我遇到了麻烦:问题: King King的答案

     // 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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 滚动时出现wpf datagrid单元格样式的问题 - issue with wpf datagrid cell style when scrolling 如何以编程方式在WPF DataGrid中选择行或单元格? - How to select a row or a cell in WPF DataGrid programmatically? 如何以编程方式将WPF Datagrid绑定到自定义对象的ObservableCollection? - How do I programmatically bind a WPF Datagrid to a ObservableCollection of custom objects? 当数据网格获得焦点时,如何关闭WPF XCeed DataGridControl并始终选择第一个单元格? - How do I turn off the WPF XCeed DataGridControl alway selecting the first cell when the datagrid gets focus? 双击WPF数据网格中的单个单元格,如何更改它的值? - How do I change the value of an individual cell in a WPF datagrid when it is double clicked? 当 SelectionMode 设置为 Cell 时,如何突出显示 WPF DataGrid 中的一行 - How do I highlight a row in the WPF DataGrid when SelectionMode is set to Cell 如何将值从DataGrid:Cell绑定到DataTrigger - How to bind value from DataGrid:Cell to DataTrigger WPF DataGrid - 如何设置正确的 DataTrigger 绑定到单元格的数据源(而不是行的源) - WPF DataGrid - How to setup correct DataTrigger binding to cell's data source (and not row's source) WPF DataGrid - 使用 DataTrigger 将样式应用于首先显示的列 - WPF DataGrid - Apply style to whichever column is displayed as first with DataTrigger 以编程方式在WPF中编辑DataGrid单元格 - Programmatically edit a datagrid cell in wpf
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM