gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 01:09:05 24 4
gpt4 key购买 nike

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

最佳答案

一种非常接近您所寻找内容的方法是将“私有(private)”样式从 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>

这样你就不能使用“私有(private)”样式,除非你明确引用那个资源字典。仅引用 ControlTemplates.xaml 资源字典将无法访问它们。

关于c# - 如何创建仅存在于 ResourceDictionary 上下文中的样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7771328/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com