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

Ala*_*ain 5 c# wpf xaml dictionary .net-3.5

如何创建仅存在于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>
Run Code Online (Sandbox Code Playgroud)

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

<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>
Run Code Online (Sandbox Code Playgroud)

Eri*_*ebo 6

一种非常接近您正在寻找的方法是将"私有"样式移动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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

然后窗口的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>
Run Code Online (Sandbox Code Playgroud)

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