简体   繁体   English

代码背后的WPF DataGrid样式

[英]WPF DataGrid Style in code behind

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. 但我不知道如何为“RowStyle”属性做同样的事情,该属性本身也有一个样式。 And I am also getting the following error when setting the BorderBrushProperty: 我在设置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")));

}

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 . <Style ...>只是System.Windows.Style The same goes for Setter , Trigger , you name it. SetterTrigger也是如此 ,你可以这么说。

The only gotcha comes from the ContentProperty attribute which is the default property assigned, for example when you do: 唯一的问题来自ContentProperty属性,这是分配的默认属性,例如当您执行以下操作时:

<TextBlock>My text here!</TextBlock>

It sets the TextBlock.Text property to "My text here!" 它将TextBlock.Text属性设置为"My text here!" , because the TextBlock class is marked with the attribute [ContentProperty("Text")] ,因为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#, 从这里你应该能够将任何XAML转换为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#. 但是,有必要注意的是,您无法将任何C#映射到XAML,例如,您无法在XAML中创建动态故事板,但您可以在C#中。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM