c# – 如何创建仅存在于ResourceDictionary上下文中的Style
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c# – 如何创建仅存在于ResourceDictionary上下文中的Style,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2825字,纯文字阅读大概需要5分钟。
内容图文
![c# – 如何创建仅存在于ResourceDictionary上下文中的Style](/upload/InfoBanner/zyjiaocheng/799/7a0c9586748b4bdeb866861f7c8b6dd1.jpg)
如何创建仅存在于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>
解决方法:
一种非常接近您正在寻找的方法是将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资源字典就无法访问它们.
内容总结
以上是互联网集市为您收集整理的c# – 如何创建仅存在于ResourceDictionary上下文中的Style全部内容,希望文章能够帮你解决c# – 如何创建仅存在于ResourceDictionary上下文中的Style所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。