2012-10-24 125 views
3

我有我的WPF应用程序DataGrid的一个XAML代码风格,我现在写的是从DataGrid中继承的自定义控制的想在代码应用以下样式背后:WPF 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'." 

任何人都可以帮我解决吗?

感谢名单

UPDATE:

我的代码更新到下面的解析错误:

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

} 
+0

您是否尝试对非空值'Color'进行常规转换?即:'(Color)ColorConverter.ConvertFromString(“#FFDDDDDD”)' –

+0

BorderBrushProperty.OverrideMetadata(typeof(DionysusDataGrid),new FrameworkPropertyMetadata((Color)ColorConverter.ConvertFromString(“#FFDDDDDD”)));给出相同的错误。 –

+0

确保您使用颜色类型的正确名称空间,并根据BorderBrush的类型进行检查。 –

回答

6

要使风格在后面的代码,一些一般的规则:

您在XAML中输入的任何内容在旧版C#中都有相应的内容:

<Style ...>只是System.Windows.StyleSetterTrigger也是如此,您可以将其命名。

唯一的疑难杂症来自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#中使用。

+0

thanx很多@Baboon !!这有助于......我现在大部分工作已经完成并且正在努力,我唯一挣扎的部分是DataGridCell的模板。 TemplateBinding如何在代码中完成? –

+0

@ChrisjanL我的谷歌搜索发现:http://www.codeproject.com/Tips/240670/WPF-TemplateBinding-in-code。应该工作正常,我猜。 –

+0

过度思考一个简单的解决方案的经典案例。我在资源字典中使用了这种样式,可以简单地使用“this.Style = Application.Current.Resources [typeof(DataGrid)]作为样式;”。但是,感谢所有的帮助,如果我需要从代码背后设置样式,我对如何实现这一点有了更好的理解! –