Topic:
Styling Tips.
标题: wpf – Styling Tips
假设我们有如下的Style定义:
Suppose
that we have a style that is defined like this:
-
<Style x:Key="NomuraTreeViewStyle" TargetType="{x:Type TreeView}">
-
<Setter Property="SnapsToDevicePixels" Value="True"/>
-
<Setter Property="OverridesDefaultStyle" Value="True"/>
-
<Setter Property="FocusVisualStyle" Value="{DynamicResource NomuraFocusVisualStyle}"/>
-
<Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto"/>
-
<Setter Property="Template">
-
<Setter.Value>
-
<ControlTemplate TargetType="{x:Type TreeView}">
-
<Border
-
x:Name="Border"
-
Background="{DynamicResource DefaultBackgroundBrush}"
-
BorderThickness="0" >
-
<ScrollViewer
-
Margin="0"
-
x:Name="PART_ContentHost"
-
Template="{DynamicResource NomuraScrollViewerControlTemplate}"
-
VerticalAlignment="Top">
-
<ItemsPresenter/>
-
</ScrollViewer>
-
</Border>
-
</ControlTemplate>
-
</Setter.Value>
-
</Setter>
-
</Style>
并且,我们定义了一个TreeView实例,代码如下.
And we have
some TreeView instance, and the code is as follow.
-
<TreeView
-
ItemsSource="{Binding SelectedAliasViewModel.Items}">
-
</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.
-
<TreeView
-
ItemsSource="{Binding SelectedAliasViewModel.Items}">
-
<TreeView.Resources>
-
<!--Temporary workaround until the TreeView control template in OnyxDark gets fixed -->
-
<SolidColorBrush x:Key="DefaultBackgroundBrush" Color="#FF333333"/>
-
</TreeView.Resources>
-
</TreeView>
但是,这毕竟是一个hack,你可以重新定义该template.
However, this is a hack, you can as
well do this:
-
<TreeView.Resources>
-
<Style TargetType="{x:Type TreeView}" BasedOn="{StaticResource NomuraTreeViewStyle}">
-
<Setter Property="Template">
-
<Setter.Value>
-
<ControlTemplate TargetType="{x:Type TreeView}">
-
<Border
-
x:Name="Border"
-
Background="{DynamicResource DefaultSubBackgroundBrush}"
-
BorderThickness="0" >
-
<ScrollViewer
-
Margin="0"
-
x:Name="PART_ContentHost"
-
Template="{DynamicResource NomuraScrollViewerControlTemplate}"
-
VerticalAlignment="Top">
-
<ItemsPresenter/>
-
</ScrollViewer>
-
</Border>
-
</ControlTemplate>
-
</Setter.Value>
-
</Setter>
-
</Style>
-
</TreeView.Resources>
为了引用一些键值名字,这样做。
To use the Static
NomuraTreeViewStyle, you can use the following.
-
<ResourceDictionary>
-
<ResourceDictionary.MergedDictionaries>
-
<ResourceDictionary Source="pack://appplication:,,,/Nomura.Desktop.Themes.OnyxDark;component/Controls/TreeViewStyle.xaml"></ResourceDictionary>
-
</ResourceDictionary.MergedDictionaries>
-
</ResourceDictionary>
References:
阅读(251) | 评论(0) | 转发(0) |