在WPF中,如何在设计视图中将样式应用于UserControl?
我通常在选项卡下的 App.xml 中定义样式和控件模板。 因此,在设计 UI 时,我可以在带有 .NET3.5 的 Visual Studio 2008 中看到具有在设计视图中应用的样式的 UI。
但是,在另一种情况下,有时我仅将 UserControl 设为项目,在这种情况下,没有 App.xml,因此 UI 在设计视图中显示为默认外观。很难知道运行时的实际情况。
有没有办法将样式也应用于 UserControl? 如果有一种方法可以在应用程序项目和 UserControl 项目之间共享相同的样式和模板,那就完美了!
如果有解决方案请告诉我。
I usually define style and control template in App.xml under tab.
So, while designing the UI, I can see the UI with the style applied in design view in Visual Studio 2008 with .NET3.5.
However, in another case, sometimes I make UserControl only project and in that case, there's no App.xml so the UI is appeared as default appearance in design view. It's hard to know actual looking in runtime.
Is there any way to apply style to UserControl, too?
If there's a way to share same style and template between application project and UserControl project, that would be perfect!
Please let me know if there's a solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将样式保存在单独的 ResourceDictionary 中您的控制项目。然后,您需要在顶部的资源块中合并字典每个用户控件:
这种方法的问题当然是您失去了应用程序级样式给您带来的任何好处(例如不同应用程序的样式不同),因为现在样式全部由底层控件决定。
You can keep your styles in a separate ResourceDictionary in your control project. Then you need to Merge Dictionaries in the Resources block at the top of every user control:
The problem with this approach is of course that you lose any benefit that App-level styles would give you (like styling differently for different applications) because now the styles are all determined by the underlying control.