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

发布于 2024-12-10 06:26:21 字数 1184 浏览 0 评论 0原文

如何创建仅存在于 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>

How can I create a style that only exists within the context of a ResourceDictionary, but not in the context of controls that include that ResourceDictionary?

For instance, I want to be able to have a ResourceDictionary that looks like this:

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

And then in some other control or window, I want to be able to go:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南…巷孤猫 2024-12-17 06:26:21

非常接近您要查找的内容的一种方法是将“私有”样式从 ControlTemplates.xaml 移动到它们自己的 ResourceDictionary 中,然后从在 ControlTemplates.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>

这样您就不能使用“私有”样式除非您明确引用该资源字典。仅通过引用 ControlTemplates.xaml 资源字典无法访问它们。

One way to get quite close to what you are looking for is to move the "private" styles from ControlTemplates.xaml into their own ResourceDictionary, and then reference that resource dictionary from within the control templates in ControlTemplates.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>

Then the xaml for the window would look like this:

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

This way you could not use the "private" styles unless you explicitly reference that resource dictionary. They will not be accessible by just referncing the ControlTemplates.xaml resource dictionary.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文