WPF 用户控件资源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13210352/
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
WPF UserControl Resources
提问by LastBye
I made a ResourceDictionaryin a WPF User Control Assembly. I Want to be able to use this across this UserControland have all the styles in this separated file.
我在 WPF 用户控制程序集中制作了一个ResourceDictionary。我希望能够在这个UserControl 中使用它,并在这个单独的文件中拥有所有样式。
The ResourceDictionary:
资源字典:
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="c1BtnX1">
<Setter Property="Background" Value="Bisque"></Setter>
</Style>
</ResourceDictionary>
It's Address is The User Control Assembly Resources/mainResX.xaml and the View is in the same assembly/Views/view.xaml
它的地址是 The User Control Assembly Resources/mainResX.xaml 和 View 在同一个 assembly/Views/view.xaml
The usage I think could be:
我认为的用法可能是:
<Border Style="{StaticResource ResourceKey=c1BtnX1}"
BorderBrush="Black"
Width="20"
Height="20">
<TextBlock Text="X" />
</Border>
Also I tried the below code inside the UserControl, to define Per Control Resources but this way also seems it couldn't find the resources.
我还尝试在 UserControl 中使用以下代码来定义每个控件资源,但这种方式似乎也找不到资源。
<UserControl ... >
<UserControl.Resources>
<ResourceDictionary Source="../Resources/mainResX.xaml" />
</UserControl.Resources>
Where and How should I place/Define this ?
我应该在哪里以及如何放置/定义它?
回答by Berryl
I can't tell what your file structure is from info provided.
我无法从提供的信息中判断您的文件结构是什么。
If the resource.xaml and control.xaml are in the same folder of the same assembly, you would just reference mainResX.xaml without "/Resources" first; otherwise you need to account for the file structure somehow.
如果resource.xaml 和control.xaml 位于同一个程序集的同一个文件夹中,则只需先引用mainResX.xaml 而不先引用“/Resources”;否则你需要以某种方式解释文件结构。
Are they in the same assembly? You can 'walk' the tree up with as many "../" strings prepended to the location as needed, and the in using the folders (ie, "../Resources/mainResX.xaml")
他们在同一个程序集中吗?您可以根据需要将尽可能多的“../”字符串“走”到该位置,并使用文件夹(即“../Resources/mainResX.xaml”)
If they are in different assemblies, you need to specify a pack uri. You can actually always do this, although it is a little cumbersome when not necessary. Here is an example
如果它们在不同的程序集中,则需要指定包 uri。您实际上可以随时执行此操作,尽管在不必要时会有点麻烦。这是一个例子
<ResourceDictionary Source="pack://application:,,,/MyAssembly.Wpf;component/Resources/mainResX.xaml" />
HTH,
Berryl
HTH,
贝瑞尔
回答by Joe
Personally I like to use my App.xaml to specify a "MergedDirectory" of XAML files containing styles that I use globally in my app. I usually have a "DefaultStyles.xaml" to set any global style (like when you want all textboxes in the app to look the same without specifying a style). Then I have a "Styles.xaml" to set specific styles, or you could even have one xaml per control type if you really have a bunch of them...
我个人喜欢使用我的 App.xaml 来指定 XAML 文件的“MergedDirectory”,其中包含我在应用程序中全局使用的样式。我通常有一个“DefaultStyles.xaml”来设置任何全局样式(例如,当您希望应用程序中的所有文本框在不指定样式的情况下看起来都相同时)。然后我有一个“Styles.xaml”来设置特定的样式,或者如果你真的有一堆它们,你甚至可以每个控件类型有一个xaml......
The fact that you place these under app.xaml makes them global to your app and don't require you to constantly re-specify the paths and dictionaries. Of course this may not fit all coding situations, but for me it's a time saver.
将它们放在 app.xaml 下的事实使它们对您的应用程序是全局的,并且不需要您不断地重新指定路径和字典。当然,这可能不适合所有编码情况,但对我来说这是一个节省时间的方法。

