sed*_*vav 9 wpf user-controls resourcedictionary
有一个WPF用户控件库和两个(或更多)用户控件.我需要在两个用户控件中使用相同的样式.我该如何分享这种风格?例如:
这是风格:
<Style x:Key="customLabelStyle" TargetType="Label">
...
</Style>
Run Code Online (Sandbox Code Playgroud)
用户控制A:
<UserControl x:Class="Edu.Wpf.Example.UserControlA"
...xmlns stuff... >
<Grid>
... some xaml markup...
<Label Style="{StaticResource customLabelStyle}"/>
</Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud)
UserControl B:
<UserControl x:Class="Edu.Wpf.Example.UserControlB"
...xmlns stuff... >
<Grid>
... some another xaml markup...
<Label Style="{StaticResource customLabelStyle}"/>
</Grid>
</UserControl>
Run Code Online (Sandbox Code Playgroud)
那么如何在不涉及应用程序app.xaml资源字典的情况下在库中的用户控件之间共享此样式?
UPDATE
我可以将Themes\Generic.xaml添加到我的库中并在那里定义样式.但在这种情况下,我必须使用ComponentResourceKey作为样式的关键.对?它很长而且不是很方便的表达......
Esp*_*dbø 14
假设您有一个定义颜色的资源,如下所示:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Color A="#FF" R="#FF" G="#22" B="#11" x:Key="MyRed"/>
<Color A="#FF" R="#00" G="#FF" B="#21" x:Key="MyGreen"/>
<Color A="#FF" R="#00" G="#22" B="#FF" x:Key="MyBlue" />
<SolidColorBrush x:Key="MyGreenBrush" Color="{StaticResource MyGreen}"/>
<SolidColorBrush x:Key="MyRedBrush" Color="{StaticResource MyRed}"/>
<SolidColorBrush x:Key="MyBlueBrush" Color="{StaticResource MyBlue}"/>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
另一个定义了一些基本样式:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style TargetType="{x:Type TextBlock}" x:Key="PocTextBlock">
<Setter Property="FontSize" Value="16"/>
</Style>
<Style TargetType="{x:Type TextBox}" x:Key="MyTextBox">
<Setter Property="FontSize" Value="20"/>
<Setter Property="Foreground" Value="{DynamicResource MyGreenBrush}"/>
</Style>
<Style TargetType="{x:Type TextBlock}" x:Key="MyResultTextBlock">
<Setter Property="FontSize" Value="16"/>
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="Foreground" Value="{DynamicResource MyGreenBrush}"/>
</Style>
<Style TargetType="{x:Type Border}" x:Key="MyBorder">
<Setter Property="BorderBrush" Value="{DynamicResource MyGreenBrush}"/>
<Setter Property="BorderThickness" Value="4"/>
<Setter Property="CornerRadius" Value="5"/>
</Style>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)
然后,您可以将资源添加到App.xaml的Application.Resources标记中,如下所示:
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="OtherStyles.xaml"/>
<ResourceDictionary Source="Colors.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
Run Code Online (Sandbox Code Playgroud)
然后,在所有UserControl中,您可以将样式或画笔用作StaticResources,如示例代码所示.
归档时间: |
|
查看次数: |
10930 次 |
最近记录: |