wpf - 在 WPF 用户控件库中的 UserControl 之间共享资源的最简单方法是什么?

标签 wpf user-controls resourcedictionary

其中有一个WPF用户控件库和两个(或更多)用户控件。我需要在两个用户控件中使用相同的样式。我如何分享这种风格? 例如:

这是风格:

<Style x:Key="customLabelStyle" TargetType="Label">
    ...
</Style>

用户控制A:

<UserControl x:Class="Edu.Wpf.Example.UserControlA"
   ...xmlns stuff... >
   <Grid>
      ... some xaml markup...
      <Label Style="{StaticResource customLabelStyle}"/>
   </Grid>
</UserControl>

用户控件B:

 <UserControl x:Class="Edu.Wpf.Example.UserControlB"
   ...xmlns stuff... >
   <Grid>
      ... some another xaml markup...
      <Label Style="{StaticResource customLabelStyle}"/>
   </Grid>
</UserControl>

那么如何在库中的用户控件之间共享此样式而不涉及应用程序 app.xaml 资源字典?

更新

我可以将 Themes\Generic.xaml 添加到我的库中并在那里定义样式。但在这种情况下,我必须使用 ComponentResourceKey 作为样式的键。正确的?它很长而且不太方便表达......

最佳答案

假设您有一个定义颜色的资源,如下所示:

<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>

还有一个定义一些基本样式,如下所示:

<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>

然后,您可以将资源添加到 App.xaml 的 Application.Resources 标记,如下所示:

<Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="OtherStyles.xaml"/>
                <ResourceDictionary Source="Colors.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

然后,在所有用户控件中,您可以使用样式或画笔作为 StaticResources,如示例代码所示。

关于wpf - 在 WPF 用户控件库中的 UserControl 之间共享资源的最简单方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6768261/

相关文章:

WPF 用户控件事件触发两次

wpf - 合并字典和资源查找

c# - AdornerLayer.GetAdornerLayer() 为面板中的所有控件返回 NULL

c# - 如何使在 wpf 窗口中运行的应用程序忽略单击或鼠标事件

wpf - 如何更改 wpf 中按钮的内容 "color"

c# - 在asp中动态添加控件时更改母版页中布局的高度

c# - Xaml 资源中的通用类型

asp.net - 在 iOS 框架中创建类似 ASP.Net 的用户控件

wpf - 如何通过 generic.xaml 中定义的键访问资源

wpf - 将标记扩展编写为嵌套元素