WPF控件作为资源字典中的StaticResource,用于多个WPF Windows?
我有一个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--> 我现在在2个不同的Windows .xaml文件中使用上面的按钮控件绑定到ContentControl控件的Content属性,其中每个Window都有自己的DataContext,因此每个窗口应根据其ViewModel的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> 但是,问题是两个Window都显示了BoundText属性的相同值,这意味着两个WPF Windows都具有相同的资源按钮控制实例,在Windows中都使用. 如何解决此问题,以便每个窗口从资源获取单独的按钮控件,并仍然从自己的ViewModel显示BoundText属性的不同值? 编辑:
您是否尝试使用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> 欲了解更多信息,请阅读here. 如果这不起作用,您可以在资源中存储模板,而不是按钮,并使用窗口内的ContentControl来显示它. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- 在WINDOWS上编译Objective-C时出现错误“Foundation.h:找不
- windows x64函数调用约定(function call convention)
- Windows – Vista上的VNC
- 在Windows中叠加图标
- .net – 禁用Windows Mobile 6.5中的菜单栏
- windows – 防止以纯文本形式获取IIS物理路径凭据(使用powe
- windows – WMI ProcessorType
- windows-8 – windows 8 app分享魅力挂起“从{AppName}获取
- winforms – Windows窗体中WebBrowser控件的浏览器版本是什
- kendo-ui – 如何从窗口内容中关闭Kendo窗口?
- windows-server-2008-r2 – Windows防火墙在重启
- windows-phone-7.1 – 删除windows phone芒果中的
- windows – OpenGL GLEW MinGW应用程序链接问题
- 通过Windows命令行启动网站
- windows-phone-7 – 有没有办法在Windows Phone
- .net – System.ServiceModel.DomainServices.En
- Windows aero peek API
- 如何在Windows中签出和构建UIAutomator源代码
- 窗口 – 当开始与Qt框架,什么选择 – 小部件或QT
- Windows – 从批处理文件在新窗口中启动进程