代码后面的 WPF DataGrid 样式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13045707/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 05:55:35  来源:igfitidea点击:

WPF DataGrid Style in code behind

c#wpfxaml

提问by Chrisjan Lodewyks

I have a xaml style for datagrids in my WPF application, I am now writing a custom control that inherits from DataGrid an would like to apply the following style in code behind:

我的 WPF 应用程序中有一个用于数据网格的 xaml 样式,我现在正在编写一个继承自 DataGrid 的自定义控件,并希望在后面的代码中应用以下样式:

<Style TargetType="DataGrid">

    <!-- Make the border and grid lines a little less imposing -->
    <Setter Property="BorderBrush" Value="#DDDDDD" />
    <Setter Property="HorizontalGridLinesBrush" Value="#DDDDDD" />
    <Setter Property="VerticalGridLinesBrush" Value="#DDDDDD" />

    <Setter Property="RowStyle">
        <Setter.Value>
            <Style TargetType="DataGridRow">
                <Style.Triggers>
                    <!-- Highlight a grid row as the mouse passes over -->
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" Value="Lavender" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Setter.Value>
    </Setter>
    <Setter Property="CellStyle">
        <Setter.Value>
            <Style TargetType="DataGridCell">
                <Style.Triggers>
                    <!-- Highlight selected rows -->
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="Background" Value="Lavender" />
                        <Setter Property="BorderBrush" Value="Lavender" />
                        <Setter Property="Foreground" Value="Black" />
                    </Trigger>
                    <!--StartsEditingOnMouseOver-->
                    <!--<Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="IsEditing" Value="True" />
                    </Trigger>-->
                </Style.Triggers>

                <EventSetter Event="PreviewMouseLeftButtonDown" Handler="DataGridCell_PreviewMouseLeftButtonDown" />
                <EventSetter Event="PreviewTextInput" Handler="DataGridCell_PreviewTextInput" />

                <!-- Add some padding around the contents of a cell -->
                <Setter Property="Padding" Value="4,3,4,3" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="DataGridCell">
                            <Border Padding="{TemplateBinding Padding}" 
                            Background="{TemplateBinding Background}">
                                <ContentPresenter />
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Setter.Value>
    </Setter>
</Style>

What I have so far is the following code:

到目前为止,我所拥有的是以下代码:

static DionysusDataGrid()
{

  BorderBrushProperty.OverrideMetadata(typeof(DionysusDataGrid), new FrameworkPropertyMetadata(ColorConverter.ConvertFromString("#FFDDDDDD") as Color?));
  HorizontalGridLinesBrushProperty.OverrideMetadata(typeof(DionysusDataGrid), new FrameworkPropertyMetadata(ColorConverter.ConvertFromString("#FFDDDDDD") as Color?));
  VerticalGridLinesBrushProperty.OverrideMetadata(typeof(DionysusDataGrid), new FrameworkPropertyMetadata(ColorConverter.ConvertFromString("#FFDDDDDD") as Color?));

}

But I have no idea how to do the same for the "RowStyle" property which also has a style in itself. And I am also getting the following error when setting the BorderBrushProperty:

但是我不知道如何对本身也有样式的“RowStyle”属性执行相同的操作。而且我在设置 BorderBrushProperty 时也收到以下错误:

Default value type does not match type of property 'BorderBrush'."

Can anyone help me out?

谁能帮我吗?

Thanx

谢谢

UPDATE:

更新:

I resolved the error by updating the code to the following:

我通过将代码更新为以下内容解决了该错误:

    static DionysusDataGrid()
{

  BrushConverter converter = new BrushConverter();

  BorderBrushProperty.OverrideMetadata(typeof(DionysusDataGrid), new FrameworkPropertyMetadata((System.Windows.Media.Brush)converter.ConvertFromString("#FFDDDDDD")));
  HorizontalGridLinesBrushProperty.OverrideMetadata(typeof(DionysusDataGrid), new FrameworkPropertyMetadata((System.Windows.Media.Brush)converter.ConvertFromString("#FFDDDDDD")));
  VerticalGridLinesBrushProperty.OverrideMetadata(typeof(DionysusDataGrid), new FrameworkPropertyMetadata((System.Windows.Media.Brush)converter.ConvertFromString("#FFDDDDDD")));

}

回答by Louis Kottmann

To make the Style in code behind, a few general rules apply:

为了使代码中的样式隐藏起来,一些通用规则适用:

Anything you type in XAML has an equivalent in good old C#:

您在 XAML 中键入的任何内容在古老的 C# 中都具有等效项:

<Style ...>is just System.Windows.Style. The same goes for Setter, Trigger, you name it.

<Style ...>只是System.Windows.StyleSetterTrigger也是如此,随便你说。

The only gotcha comes from the ContentPropertyattribute which is the default property assigned, for example when you do:

唯一的问题来自ContentProperty分配的默认属性的属性,例如,当您执行以下操作时:

<TextBlock>My text here!</TextBlock>

It sets the TextBlock.Textproperty to "My text here!", because the TextBlockclass is marked with the attribute [ContentProperty("Text")]

它将TextBlock.Text属性设置为"My text here!",因为TextBlock类标记有属性[ContentProperty("Text")]

And lastly, you need to start from the most nested element when you build from C#:

最后,当您从 C# 构建时,您需要从嵌套最多的元素开始:

<Style TargetType="DataGrid">
    <Setter Property="BorderBrush" Value="#DDDDDD" />
</Style>

Becomes:

变成:

var brushConverter = new BrushConverter();

var bbSetter = new Setter(
    DataGrid.BorderBrushProperty, 
    brushConverter.ConvertFromString("#FFDDDDDD"));

var style = new Style(typeof(DataGrid));    
style.Setters.Add(bbSetter);

From this you should be able to convert any XAML to C#,
It is useful to note, though, that you can't map any C# to XAML, for example you can't make a dynamic storyboard in XAML, but you can in C#.

从这里你应该能够将任何 XAML 转换为 C#,
但需要注意的是,你不能将任何 C# 映射到 XAML,例如你不能在 XAML 中制作动态故事板,但你可以在 C# 中.