如何使一个样式,只有一个的ResourceDictionary的上下文中是否存在只有一个、文中、是否存在、样式

2023-09-07 10:00:20 作者:国产祖宗

如何创建一种风格,只是存在的ResourceDictionary的范围内,而不是在控件的背景下,包括的ResourceDictionary?

例如,我希望能够有一个ResourceDictionary中,看起来像这样:

 <! -  ControlTemplates.xaml  - >
<的ResourceDictionary>
    <! - 用于建立公开可用的模板私人地方风格 - >
        <风格X:关键=文本框的TargetType =文本框>
            < setter属性=TextWrappingVALUE =自动换行/>
        < /样式和GT;
    <! - 私人地方的东西结束 - >
    <! - 公共词典资源跟踪 - >
    <控件模板X:关键=CustomTextBox>
        <文本框样式={的StaticResource文本框}/>
    < /控件模板>
< / ResourceDictionary中>
 

然后在其他一些控制或窗口,我希望能够去:

 <窗​​口>
    < Window.Resources>
        < ResourceDictionary中源=ControlTemplates.xaml>
    < /Window.Resources>
    <电网>
        <! - 这应该工作 - >
        &所述; CustomControl模板={StaticResources CustomTextBox}>

        <  - !这不应该工作! - >
        <文本框模板={StaticResources文本框}>
    < /网格>
< /窗>
 

解决方案

一种方式来获得相当接近你正在寻找的东西是从 ControlTemplates.xaml 到自己的的ResourceDictionary ,然后引用资源字典从控制模板内ControlTemplates.xaml:

ControlTemplates.xaml:

 < ResourceDictionary中的xmlns =htt​​p://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
                    的xmlns:X =htt​​p://schemas.microsoft.com/winfx/2006/xaml>

    <! - 通过从内引用ResourceDictionary中的控件模板的
         资源也将只可用于控件模板,而不是为那些
         谁引用ControlTemplates.xaml  - >
    <控件模板X:关键=CustomTextBox>
        < ControlTemplate.Resources>
            < ResourceDictionary中源=ControlTemplatePrivateStyles.xaml/>
        < /ControlTemplate.Resources>

        <文本框样式={的StaticResource文本框}文本=一些文本/>
    < /控件模板>

< / ResourceDictionary中>
 
应用程式资源 Application Resources 使用与资源字典 ResourceDictionary 的介绍

ControlTemplatePrivateStyles.xaml:

 < ResourceDictionary中的xmlns =htt​​p://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
                    的xmlns:X =htt​​p://schemas.microsoft.com/winfx/2006/xaml>

    <风格X:关键=文本框的TargetType =文本框>
        < setter属性=TextWrappingVALUE =自动换行/>
    < /样式和GT;

< / ResourceDictionary中>
 

那么XAML的窗口是这样的:

 <窗​​口x:类=ResourceDictionaryPrivateStyle.MainWindow
    的xmlns =htt​​p://schemas.microsoft.com/winfx/2006/xaml/$p$psentation
    的xmlns:X =htt​​p://schemas.microsoft.com/winfx/2006/xaml
    的xmlns:ResourceDictionaryPrivateStyle =CLR的命名空间:ResourceDictionaryPrivateStyle
    标题=主窗口高度=350宽度=525>

    < Window.Resources>
        < ResourceDictionary中源=ControlTemplates.xaml/>
    < /Window.Resources>

    < StackPanel的>
        <  - !这工作 - >
        < ResourceDictionaryPrivateStyle:CustomControl模板={的StaticResource CustomTextBox}/>

        <! - 这是不行的,除非你明确地引用ControlTemplatesPrivateStyles.xaml在这里的窗口 - >
        <文本框文本=文本样式={的StaticResource文本框}/>
    < / StackPanel的>
< /窗>
 

此方式,您不能使用民营方式,除非你明确地引用资源字典。仅通过referncing的ControlTemplates.xaml资源字典,他们将无法访问。

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>

解决方案

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.