没有 Generic.xaml 的 WPF 自定义视图
我正在尝试实现特定于应用程序的自定义视图,但没有任何运气。这是我的问题: 我需要一个自定义视图,因为我希望用户能够在运行时动态切换视图。我需要一个自定义视图(而不是仅数据模板),因为列表视图布局以及控件模板和数据模板都必须更改。 所有教程都提到要实现从 viewbase 派生的自定义类,并重写 DefaultStyleKey 和 ItemContainerDefaultStyleKey 以返回 generic.xaml 中定义的 ComponentResourceKey。然而,问题是我正在尝试创建几个非常特定于该应用程序的视图。某些画笔和字体在应用程序中将保持一致,并且自定义视图将使用它们。即我有应用程序级别的 Forebrush、Shadowbrush、Deepshadowbrush、TextDecorator 等,我希望视图使用这些。如果视图将在外部 generic.xaml 中定义,则绑定到这些视图将是非常复杂的标记。此外,无论如何,它都会使它们特定于应用程序(如果它们绑定到这些画笔)。 任何人都知道如何在应用程序内部定义可以在运行时更改的视图样式?
I am trying to implement custom views that are specific to an application without any luck. Here is my problem:
I need a custom view because I would like for the user to be able to switch views dynamically at runtime. I need a custom view (as opposed to only datatemplates) because the listview layout has to change as well as the Control template and the data template.
All of the turorials say to implement Custom classes that derive from viewbase and override the DefaultStyleKey and ItemContainerDefaultStyleKey to return a ComponentResourceKey defined in generic.xaml. However, the problem is that I am trying to create several views that are very specific to that application. certain brushes and fonts will be consistant accross the application and the custom views will use these. i.e. I have application level Forebrush, Shadowbrush, Deepshadowbrush, TextDecorator, etc. and I want the views to use these. If the view will be defined in an external generic.xaml it will be very convoluted markup to bind to these. And besides, it would make them application specific anyway (if they bind to these brushes).
Anyone have an idea how to define styles for views internaly in the application that will be able to be changed at runtime?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我对您的详细信息有点困惑,但是您可以在运行时设置 ListView 的样式...其中 CustomStyle 是您想要应用于 ListView 的预定义样式。
DefaultStyleKey 适用于自定义控件(这与用户控件不同)。假设您想要一个名为 Widget 的新控件。您不需要为该 Widget 定义 DefaultStyleKey,因为它没有定义默认样式。 UserControl 是 Control 的集合,因此它没有预定义的样式。
此外,您可以创建一个
ResourceDictionary
来分解您的样式。然后您可以通过 App.xaml 合并它们......I'm slightly confused on your details, however you can set the Style of a ListView at runtime as such...where CustomStyle is a predefined style that you want to apply to the ListView.
The DefaultStyleKey is applicable to a custom Control (this is different then a UserControl). So say you want a new Control called a Widget. You would need ot define the DefaultStyleKey for that Widget since it does not have a default style defined. A UserControl is a collection of Controls, therefore it does not have a pre-defined style as such.
In addition you can create a
ResourceDictionary
to break apart your styles. You can then merge them via the App.xaml as such...