簡體   English   中英

以編程方式添加DataTrigger時,如何繼承WPF DataGrid單元格樣式?

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

我正在編寫一個利用DataGrid控件的WPF應用程序。 我正在使用MaterialDesign主題來設置應用程序的樣式,這將給用戶帶來很好的外觀。

但是,由於復雜的原因,我不會在這里進行介紹,我需要以編程方式將這些列添加到dataGrid中。 對於某些列,我還對列進行樣式設置,以紅色突出顯示通過/失敗。 當我這樣做時,我會松開該列的材料設計所提供的“某些樣式”。 即水平和垂直對齊。

顯示第三列對齊方式的DataGrid未被拾取

上面的代碼如下:

            // 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方法?

在這個問題上浪費了很多時間之后,我遇到了丹尼·貝克特(Danny Beckett)的類似問題和金國王(King King)的回答。 通過使用他的答案並將其應用於特定的單元格,我遇到了麻煩:問題: 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM