[英]WPF/C#/MVVM: Need Some Help Implementing Custom View Options for User Control
The project I am currently working on is a text editor type app.我目前正在从事的项目是一个文本编辑器类型的应用程序。 I am wondering how I can handle custom view options.
我想知道如何处理自定义视图选项。 eg.
例如。 font family, size, bold, colors for my
TextBox
.我的
TextBox
的字体系列、大小、粗体、颜色。 My Editor Tab is a EditorTabViewModel
within the View is a custom user control MarkdownEditor
.我的 Editor Tab 是 View 内的
EditorTabViewModel
是自定义用户控件MarkdownEditor
。 Its basically a text box with some buttons for bold/italic etc. I am wondering how can I somehow set options for the custom usercontrol/editor from like a OptionsView
of my app?它基本上是一个带有一些粗体/斜体等按钮的文本框。我想知道如何以某种方式从我的应用程序的
OptionsView
中设置自定义用户控件/编辑器的选项?
the way I am rendering the editor is我渲染编辑器的方式是
<Window.Resources>
<DataTemplate DataType="{x:Type vm:EditorTabViewModel}">
<me:MarkdownEditor />
</DataTemplate>
</Window.Resources>
MarkdownEditor
is a user control that exposes public display properties for setting fonts, colors etc. MarkdownEditor
是一个用户控件,它公开了用于设置字体、颜色等的公共显示属性。
UPDATE : also since there can be many MarkdownEditor
s in the app, I want the options to be global更新:由于应用程序中可能有许多
MarkdownEditor
,我希望选项是全局的
There is nothing keeping you from having View constructs in your ViewModel... especially if you consider the ViewModel to be a model of the View (as opposed to a view of the model).没有什么可以阻止您在 ViewModel 中使用 View 构造......特别是如果您认为 ViewModel 是 View 的模型(而不是模型的视图)。 Not all purists think this is a good idea.
并非所有纯粹主义者都认为这是一个好主意。 Most pragmatists, however, do.
然而,大多数实用主义者都这样做。
Having properties like FontWeight, FontColor, FontSize, etc in your EditorTabViewModel
would work for you here, and you can then bind them in the properties of the MarkdownEditor
.在
EditorTabViewModel
中拥有 FontWeight、FontColor、FontSize 等属性在这里对你有用,然后你可以将它们绑定到MarkdownEditor
的属性中。 It would allow for you to dynamically change the UI properties of the text via the ViewModel.它允许您通过 ViewModel 动态更改文本的 UI 属性。
If you are a purist, and you don't like having the ViewModel know about WPF specific View constructs, you could create your own text formatting class, called something like TextFormatting
.如果您是一个纯粹主义者,并且您不喜欢让 ViewModel 了解 WPF 特定的 View 构造,您可以创建自己的文本格式化类,称为
TextFormatting
之类的东西。 TextFormatting
can include your own representation of the information you want to communicate (bold, italics, color, font size, etc). TextFormatting
可以包含您自己想要传达的信息的表示形式(粗体、斜体、颜色、字体大小等)。 You can then bind that TextFormatting
object to the MarkdownEditor
using an attached property/behavior.然后,您可以使用附加的属性/行为将该
TextFormatting
对象绑定到MarkdownEditor
。 The attached property/behavior would be responsible for mapping the formatting representation to the WPF representation.附加的属性/行为将负责将格式表示映射到 WPF 表示。
Doing it the second way is very useful if you have different Views (WinForms/WPF) that might use the same ViewModel, since it remains view engine agnostic.如果您有可能使用相同 ViewModel 的不同视图 (WinForms/WPF),则第二种方式非常有用,因为它仍然与视图引擎无关。 It also adds a layer of complexity that the first option does not.
它还增加了第一个选项没有的复杂性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.