wpf 如何使用来自单独的 xaml 文件的样式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14281093/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use styles from separate xaml files
提问by Sakamoto Kazuma
I have a styles.xaml file that lists a set of colors. These colors define how certain elements within one part of the application are shown, and thus are used through a converter.
我有一个 style.xaml 文件,其中列出了一组颜色。这些颜色定义了应用程序某一部分中某些元素的显示方式,因此可以通过转换器使用。
I would like to create a legend of these colors in another part of the application, and have a toggle button list that I'd like to set the background colors to the colors defined in the styles.xaml.
我想在应用程序的另一部分创建这些颜色的图例,并有一个切换按钮列表,我想将背景颜色设置为styles.xaml 中定义的颜色。
Would I need to somehow include the styles.xaml file into the xaml file defining the toggle buttons? Or is there some way I can bind directly to these color values?
我是否需要以某种方式将styles.xaml 文件包含到定义切换按钮的xaml 文件中?或者有什么方法可以直接绑定到这些颜色值?
回答by chameleon86
Add styles.xaml to App.xaml
将styles.xaml 添加到App.xaml
<Application.Resources>
<ResourceDictionary >
<ResourceDictionary.MergedDictionaries >
<ResourceDictionary Source="styles.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
回答by Athafoud
NoteAttribution for the following content / answer should go to @Chris Schaller. This answer's content was originally posted as an edit to @chameleon86 answerand was rejected(see also thismeta). However I think, this is some valuable content and so I am 'reposting' it.
注意以下内容/答案的归属应转至@Chris Schaller。这个答案的内容最初是作为对@chameleon86答案的编辑发布的,但被拒绝了(另见这个元)。但是我认为,这是一些有价值的内容,所以我正在“重新发布”它。
To make the definitions in styles.xaml available to all XAML within the app, add styles.xaml to App.xaml
要将styles.xaml 中的定义提供给应用程序中的所有XAML,请将styles.xaml 添加到App.xaml
<Application.Resources>
<ResourceDictionary >
<ResourceDictionary.MergedDictionaries >
<ResourceDictionary Source="styles.xaml"/>
</ResourceDictionary.MergedDictionaries>
<!-- You can declare additional resources before or after Merged dictionaries, but not both -->
<SolidColorBrush x:Key="DefaultBackgroundColorBrush" Color="Cornsilk" />
<Style x:Key="DefaultBackgroundColor" TargetType="TextBox">
<Setter Property="Background" Value="{StaticResource DefaultBackgroundColorBrush}" />
</Style>
</ResourceDictionary>
</Application.Resources>
To understand how this works, at runtime your window, page or control will exist as child elements of the running application visual tree.
要了解这是如何工作的,在运行时您的窗口、页面或控件将作为正在运行的应用程序可视化树的子元素存在。
Your initial question noted:
您最初的问题指出:
"These colors define how certain elements within one partof the application..."
“这些颜色定义了应用程序某一部分中的某些元素如何......”
If you only need these style resources available to somexaml pages or windows, and not all of them, then you can still use this pattern to merge the local resources for a window, or for grids or other controls directly.
如果您只需要这些样式资源可用于某些xaml 页面或窗口,而不是全部,那么您仍然可以使用此模式来合并窗口的本地资源,或者直接用于网格或其他控件。
- Note that doing this makes these styles only available to child elements of the element that you declared the Resource Dictionary.
- 请注意,这样做会使这些样式仅可用于您声明资源字典的元素的子元素。
See how simple it is scope the style reference to a single grid for usage:
看看它是如何简单地将样式引用范围限定为单个网格以供使用:
<Grid>
<Grid.Resources>
<ResourceDictionary >
<ResourceDictionary.MergedDictionaries >
<ResourceDictionary Source="styles.xaml"/>
</ResourceDictionary.MergedDictionaries>
<!-- You can declare additional resources before or after Merged dictionaries, but not both -->
</ResourceDictionary>
</Grid.Resources>
<!--
Grid Content :)
-->
</Grid>

