wpf 中的 ViewModel 的技术代码?

发布于 2024-09-13 22:31:53 字数 1360 浏览 3 评论 0原文

我有一个 UserControl,其中一些自定义依赖属性绑定到 ViewModel 中的 clr 属性。 ViewModel 具有应用程序逻辑,我在其中使用 FlowDocument 处理 TextPointer/TextRange 类。

我应该将这些内容放入 UserControl 的代码隐藏中还是 ViewModel 中?

ranges.Clear();
            TextRange range = new TextRange(boundXamlDocument.ContentStart, boundXamlDocument.ContentEnd);
            foreach (var block in boundXamlDocument.Blocks)
            {
                if (block is Paragraph)
                {
                    Paragraph p = block as Paragraph;
                    //if paragraph has Strikethrough, then do not loop its inlines.
                    if (p.TextDecorations.Contains(TextDecorations.Strikethrough[0]))
                    {
                        TextRange tr = new TextRange(p.ContentStart, p.ContentEnd);
                        ranges.Add(tr);
                    }
                    else
                    {
                        foreach (var run in p.Inlines)
                        {
                            if (run.TextDecorations.Contains(TextDecorations.Strikethrough[0]))
                            {
                                TextRange tr = new TextRange(run.ContentStart, run.ContentEnd);
                                ranges.Add(tr);
                            }
                        }
                    }
                }
            }

I have a UserControl with some custom dependency properties bound to a clr property in the ViewModel. The ViewModel has application logic where I deal with the TextPointer/TextRange classes with a FlowDocument.

Should I put that stuff into the code-behind of the UserControl or in the ViewModel?

ranges.Clear();
            TextRange range = new TextRange(boundXamlDocument.ContentStart, boundXamlDocument.ContentEnd);
            foreach (var block in boundXamlDocument.Blocks)
            {
                if (block is Paragraph)
                {
                    Paragraph p = block as Paragraph;
                    //if paragraph has Strikethrough, then do not loop its inlines.
                    if (p.TextDecorations.Contains(TextDecorations.Strikethrough[0]))
                    {
                        TextRange tr = new TextRange(p.ContentStart, p.ContentEnd);
                        ranges.Add(tr);
                    }
                    else
                    {
                        foreach (var run in p.Inlines)
                        {
                            if (run.TextDecorations.Contains(TextDecorations.Strikethrough[0]))
                            {
                                TextRange tr = new TextRange(run.ContentStart, run.ContentEnd);
                                ranges.Add(tr);
                            }
                        }
                    }
                }
            }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笔落惊风雨 2024-09-20 22:31:53

只有当我的概念不能很好地适合任何常用控件(很少甚至从不),或者我有想要重用的自定义控件行为(更常见)时,我才会费心设计自定义/用户控件。

您的控件越抽象,它的可重用性就越高。不过,如果将其变得如此抽象以至于没有人会从中受益,那就太过分了:)

如果您有应用程序逻辑,那么最好尽可能在视图模型(或模型)中定义它。当该逻辑发生变化时,它不会破坏您控件的其他用户。

如果控件的某个功能不是特定于确切的演示/用户输入样式,而是特定于该控件的该实例,则您可能应该将其放入视图模型中。

编辑:

从您的评论来看,您尝试编写的代码似乎取决于 UI 元素(TextBlock 文本装饰器)。这意味着它必须而且应该出现在视图中。

I only bother to design custom/user controls when I have a concept that won't fit well into any of the usual controls (rare to never), or I have custom control behavior that I want to reuse (more common).

The more abstract your control can be, the more reusable it will be. Although, making it so abstract that no one would get any benefit from it would be doing too much :)

If you have application logic, it is best to define it in the view model (or model) when at all possible. When that logic changes, it won't break other users of your control.

If a feature of the control isn't specific to the exact presentation/user input style, and is specific to that instance of the control, you should probably put it in the view model.

Edit:

From your comments, it seems that the code you are trying to write depends on UI elements (TextBlock text decorators). This means it must and should go in the view.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文