Chinaunix首页 | 论坛 | 博客
  • 博客访问: 49381
  • 博文数量: 21
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 190
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-24 15:25
文章分类

全部博文(21)

文章存档

2013年(21)

我的朋友

分类: C#/.net

2013-07-05 12:49:38

Topic: Styling Tips.

标题: wpf – Styling Tips

假设我们有如下的Style定义:

Suppose that we have a style that is defined like this:

点击(此处)折叠或打开

  1. <Style x:Key="NomuraTreeViewStyle" TargetType="{x:Type TreeView}">
  2.         <Setter Property="SnapsToDevicePixels" Value="True"/>
  3.         <Setter Property="OverridesDefaultStyle" Value="True"/>
  4.         <Setter Property="FocusVisualStyle" Value="{DynamicResource NomuraFocusVisualStyle}"/>
  5.         <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
  6.         <Setter Property="Template">
  7.             <Setter.Value>
  8.                 <ControlTemplate TargetType="{x:Type TreeView}">
  9.                     <Border
  10.                         x:Name="Border"
  11.                         Background="{DynamicResource DefaultBackgroundBrush}"
  12.                         BorderThickness="0" >
  13.                         <ScrollViewer
  14.                             Margin="0"
  15.                             x:Name="PART_ContentHost"
  16.                             Template="{DynamicResource NomuraScrollViewerControlTemplate}"
  17.                             VerticalAlignment="Top">
  18.                             <ItemsPresenter/>
  19.                         </ScrollViewer>
  20.                     </Border>
  21.                 </ControlTemplate>
  22.             </Setter.Value>
  23.         </Setter>
  24.     </Style>


并且,我们定义了一个TreeView实例,代码如下.

And we have some TreeView instance, and the code is as follow.

点击(此处)折叠或打开

  1. <TreeView
  2.                             ItemsSource="{Binding SelectedAliasViewModel.Items}">
  3.                         </TreeView>

但是该TreeView实例的背景色设置成了DefaultApplcationBackground,你想改变该背景色,再不改变ControlTemplate的情况下, 你可以用如下的方法

But you will get a Background setting to DefaultApplcationBackground, you want to change that to something different. Here is a tip, without changing the ControlTemplate.

点击(此处)折叠或打开

  1.                         <TreeView
  2.                             ItemsSource="{Binding SelectedAliasViewModel.Items}">
  3.                             <TreeView.Resources>
  4.                                 <!--Temporary workaround until the TreeView control template in OnyxDark gets fixed -->
  5.                                 <SolidColorBrush x:Key="DefaultBackgroundBrush" Color="#FF333333"/>
  6.                             </TreeView.Resources>
  7.                         </TreeView>

但是,这毕竟是一个hack,你可以重新定义该template.

However, this is a hack, you can as well do this: 

点击(此处)折叠或打开

  1. <TreeView.Resources>
  2.                                 <Style TargetType="{x:Type TreeView}" BasedOn="{StaticResource NomuraTreeViewStyle}">
  3.                                     <Setter Property="Template">
  4.                                         <Setter.Value>
  5.                                             <ControlTemplate TargetType="{x:Type TreeView}">
  6.                                                 <Border
  7.                                                                      x:Name="Border"
  8.                                                                      Background="{DynamicResource DefaultSubBackgroundBrush}"
  9.                                                                      BorderThickness="0" >
  10.                                                     <ScrollViewer
  11.                                                                             Margin="0"
  12.                                                                             x:Name="PART_ContentHost"
  13.                                                                             Template="{DynamicResource NomuraScrollViewerControlTemplate}"
  14.                                                                             VerticalAlignment="Top">
  15.                                                         <ItemsPresenter/>
  16.                                                     </ScrollViewer>
  17.                                                 </Border>
  18.                                             </ControlTemplate>
  19.                                         </Setter.Value>
  20.                                     </Setter>
  21.                                 </Style>
  22.                             </TreeView.Resources>

为了引用一些键值名字,这样做。

To use the Static NomuraTreeViewStyle, you can use the following.


点击(此处)折叠或打开

  1. <ResourceDictionary>
  2.                                     <ResourceDictionary.MergedDictionaries>
  3.                                         <ResourceDictionary Source="pack://appplication:,,,/Nomura.Desktop.Themes.OnyxDark;component/Controls/TreeViewStyle.xaml"></ResourceDictionary>
  4.                                     </ResourceDictionary.MergedDictionaries>
  5.                                 </ResourceDictionary>
 

References:

阅读(251) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~