wpf WPF控件作为资源字典中的StaticResource,在多个WPF Windows中使用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14849639/
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 Controls as StaticResource in Resource Dictionary, used in multiple WPF Windows?
提问by VS1
I have a Button control as a resource in Resource Dictionary as below:
我有一个 Button 控件作为资源字典中的资源,如下所示:
<!--ButtonResources.xaml file-->
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Button x:Key="buttonResource" Content={Binding BoundText}/>
</ResourceDictionary>
<!--ButtonResources.xaml file-->
I now use this above button control bound to Content property of ContentControl controlsin 2 different Windows.xaml files where each Windowhas its own DataContextand thus each window should display the Contentof above button control based on its ViewModel'sBoundTextproperty value as below for each Window.
我现在在2 个不同的 Windows.xaml 文件中使用绑定到 ContentControl 控件的 Content 属性的上面的按钮控件,每个文件都有自己的,因此每个窗口应该根据每个窗口的属性值显示上面的按钮控件,如下所示。WindowDataContextContentViewModel'sBoundText
<Window x:Class="TestClass1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Window.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<ResourceDictionary Source="ButtonResources.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Window.Resources>
<Grid>
<ContentControl Content={StaticResource buttonResource}/>
</Grid>
</Window>
But, the problem is that both Window's display same value for the BoundText property which means that both the WPF Windows have sameinstance of button control from resource, used in both the Windows.
但是,问题在于两个 Window 的 BoundText 属性显示相同的值,这意味着两个 WPF Windows 都有来自资源的相同按钮控件实例,在两个 Windows 中都使用。
How can I resolve this problem such that each Window gets a separatebutton control from the resource and still display differentvalues for the BoundTextproperty from their own ViewModel?
我该如何解决这个问题,让每个 Window从资源中获取一个单独的按钮控件,并且仍然从他们自己的 ViewModel 中显示不同的BoundText属性值?
Edit:For the reason mentioned in MSDNas below, I can't use x:Shared="False" attribute to resolve this:
编辑:由于下面提到的原因MSDN,我不能使用 x:Shared="False" 属性来解决这个问题:
?The ResourceDictionary that contains the items must not be nested within another ResourceDictionary. For example, you cannot use x:Shared for items in a ResourceDictionary that is within a Style that is already a ResourceDictionary item.
? 包含项目的 ResourceDictionary 不得嵌套在另一个 ResourceDictionary 中。例如,您不能将 x:Shared 用于 ResourceDictionary 中的项目,而该项目位于已经是 ResourceDictionary 项目的 Style 中。
回答by Alex Shtof
Did you try to use the x:Sharedattribute?
您是否尝试使用该x:Shared属性?
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Button x:Shared="False" x:Key="buttonResource" Content={Binding BoundText}/>
</ResourceDictionary>
For more info read here.
欲了解更多信息,请阅读此处。
In case this does not work, you can store a template in your resource, instead of the button and use a ContentControl inside your window to display it.
如果这不起作用,您可以在您的资源中存储一个模板,而不是按钮,并在您的窗口中使用 ContentControl 来显示它。
回答by KBoek
Try:
尝试:
<Style TargetType="Button" x:Key="buttonResource">
<Setter Property="Content" Value="{Binding BoundText}" />
</Style>
<Button Style="{StaticResource buttonResource}" />

