如何创建只存在于ResourceDictionary上下文中的样式

本文关键字:ResourceDictionary 上下文 样式 存在 何创建 创建 | 更新日期: 2023-09-27 18:11:21

如何创建一个只存在于ResourceDictionary上下文中,而不存在于包含该ResourceDictionary的控件上下文中的样式?

例如,我希望能够有一个像这样的ResourceDictionary:

<!-- ControlTemplates.xaml -->
<ResourceDictionary>
    <!-- Private Local styles used to set up the publicly usable templates -->
        <Style x:Key="TextBoxes" TargetType="TextBox">
            <Setter Property="TextWrapping" Value="Wrap" />
        </Style>
    <!-- End of Private Local Stuff -->
    <!-- Public Dictionary Resources Follow -->
    <ControlTemplate x:Key="CustomTextBox">
        <TextBox Style="{StaticResource TextBoxes}" />
    </ControlTemplate>
</ResourceDictionary>

然后在其他控件或窗口中,我希望能够输入:

<Window>
    <Window.Resources>
        <ResourceDictionary Source="ControlTemplates.xaml">
    </Window.Resources>
    <Grid>
        <!-- This Should Work -->
        <CustomControl Template="{StaticResources CustomTextBox}">
        <!-- This Should NOT Work! -->
        <TextBox Template="{StaticResources TextBoxes}">
    </Grid>
</Window>

如何创建只存在于ResourceDictionary上下文中的样式

一种非常接近您正在寻找的方法是将"私有"样式从ControlTemplates.xaml移动到自己的ResourceDictionary,然后在controltemplate .xaml:

中从控件模板中引用该资源字典。

ControlTemplates.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <!-- By referencing the ResourceDictionary from within the ControlTemplate's
         resources it will only be available for the ControlTemplate and not for those
         who reference ControlTemplates.xaml -->
    <ControlTemplate x:Key="CustomTextBox">
        <ControlTemplate.Resources>
            <ResourceDictionary Source="ControlTemplatePrivateStyles.xaml" />
        </ControlTemplate.Resources>
        <TextBox Style="{StaticResource TextBoxes}" Text="Some text" />
    </ControlTemplate>
</ResourceDictionary>

ControlTemplatePrivateStyles.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style x:Key="TextBoxes" TargetType="TextBox">
        <Setter Property="TextWrapping" Value="Wrap" />
    </Style>
</ResourceDictionary>

那么窗口的xaml将看起来像这样:

<Window x:Class="ResourceDictionaryPrivateStyle.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:ResourceDictionaryPrivateStyle="clr-namespace:ResourceDictionaryPrivateStyle"
    Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <ResourceDictionary Source="ControlTemplates.xaml" />
    </Window.Resources>
    <StackPanel>
        <!-- This works -->
        <ResourceDictionaryPrivateStyle:CustomControl Template="{StaticResource CustomTextBox}" />
        <!-- This does not work, unless you explicitly reference ControlTemplatesPrivateStyles.xaml here in the window-->
        <TextBox Text="Text" Style="{StaticResource TextBoxes}" />
    </StackPanel>
</Window>

这样你就不能使用"private"样式,除非你显式地引用该资源字典。它们不能仅仅通过引用ControlTemplates来访问。Xaml资源字典。

相关文章:
  • 没有找到相关文章