VS1*_*VS1 7 wpf xaml .net-4.0 resourcedictionary staticresource
我有一个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-->
Run Code Online (Sandbox Code Playgroud)
我现在用这以上按钮控制绑定到ContentControl中控制的内容属性在2个不同的视窗的.xaml文件,其中每个Window
都有自己的DataContext
,从而每一个窗口应该显示Content
基于其上述按钮控制的ViewModel's
BoundText
属性值,如下对每个窗口.
<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>
Run Code Online (Sandbox Code Playgroud)
但是,问题是两个Window都显示了BoundText属性的相同值,这意味着两个WPF Windows都具有来自资源的相同按钮控制实例,在Windows中都使用.
如何解决此问题,以便每个窗口从资源获取单独的按钮控件,并仍然从他们自己的ViewModel显示属性的不同值?BoundText
编辑:
由于下面提到的原因MSDN
,我不能使用x:Shared ="False"属性来解决此问题:
•包含项的ResourceDictionary不得嵌套在另一个ResourceDictionary中.例如,对于已经是ResourceDictionary项的Style内的ResourceDictionary中的项,您不能使用x:Shared.
您是否尝试使用该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>
Run Code Online (Sandbox Code Playgroud)
欲了解更多信息阅读这里.
如果这不起作用,您可以在资源中存储模板,而不是按钮,并使用窗口内的ContentControl来显示它.
归档时间: |
|
查看次数: |
16327 次 |
最近记录: |