代码背后的WPF DataGrid样式

时间:2012-10-24 08:45:51

标签: c# wpf xaml

我的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>

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

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

}

但我不知道如何为“RowStyle”属性执行相同的操作,该属性本身也具有样式。我在设置BorderBrushProperty时也遇到以下错误:

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

任何人都可以帮助我吗?

感谢名单

更新:

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

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

}

1 个答案:

答案 0 :(得分:6)

要使代码中的样式落后,需要遵循一些通用规则:

您在XAML中键入的任何内容都具有相同的旧C#:

<Style ...>只是System.Windows.Style。对于SetterTrigger,您也可以这样说。

唯一的问题来自ContentProperty属性,这是分配的默认属性,例如:

<TextBlock>My text here!</TextBlock>

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

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

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

变为:

var brushConverter = new BrushConverter();

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

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

从这里你应该能够将任何XAML转换为C#,
但是,有必要注意的是,您无法将任何C#映射到XAML,例如,您无法在XAML中创建动态故事板,但您可以在C#中。