VS Extension: WPF : 使用全局方式 设置 窗体 foreground background

时间:2023-03-09 08:18:48
VS Extension: WPF : 使用全局方式 设置 窗体 foreground background

VS Extension 中 创建 WPF窗体后,总希望窗体的前后背景色和VS当前配色方案一致。

对每个control使用下列定义可以达到效果

             Background="{DynamicResource VsBrush.Window}"
Foreground="{DynamicResource VsBrush.WindowText}"

但是控件多了就很重复。

这时可以使用Style来设置全局默认配置。

<UserControl x:Class="SteIde.TestItems.TestItemsWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="800" d:DesignWidth="600"> <UserControl.Resources>
<Style x:Key="CommonStyle" TargetType="{x:Type Control}">
<Setter Property="Foreground" Value="{DynamicResource VsBrush.WindowText}" />
<Setter Property="Background" Value="{DynamicResource VsBrush.Window}" />
</Style>
<Style BasedOn="{StaticResource CommonStyle}" TargetType="Label"/>
<Style BasedOn="{StaticResource CommonStyle}" TargetType="TreeView"/>
<Style BasedOn="{StaticResource CommonStyle}" TargetType="TreeViewItem"/> </UserControl.Resources> <Grid>
。。。

以上代码,将对所有该窗体内的Label,TreeView,以及TreeView中的所有新添加的节点TreeViewItem起作用。

如果不想某个控件继承以上配置,可以在其创建时添加Style x:Null

<TextBox Style="{x:Null}" />

参考:

http://*.com/questions/802658/can-you-define-multiple-targettypes-for-one-xaml-style

http://www.cnblogs.com/Zhouyongh/archive/2011/08/01/2123610.html