WPF/MVVM:RichTexBox 作为数据网格单元格编辑器,将格式化代码放入 ViewModel 中?

发布于 2024-09-12 15:49:53 字数 1855 浏览 4 评论 0原文

我真的很想知道我可以在 ViewModel 中放入什么。

如果需要对其进行测试,可能有人会说...

但是,当 CaretPosition(用于获取所选文本)之类的属性不是依赖

属性时,最好忘记 ViewModel。

目前我在代码隐藏中有这样的内容:

private void rtbContent_SelectionChanged(object sender, System.Windows.RoutedEventArgs e)
        {
            RichTextBox rtb = sender as RichTextBox;

            TextPointer tpForward = rtb.CaretPosition.GetNextContextPosition(LogicalDirection.Forward);
            TextPointer tpBackward = rtb.CaretPosition.GetNextContextPosition(LogicalDirection.Backward);

            if (tpForward != null && tpBackward != null)
            {
                DependencyObject nextObj = tpForward.GetAdjacentElement(LogicalDirection.Forward);
                DependencyObject prevObj = tpBackward.GetAdjacentElement(LogicalDirection.Backward);

                TextElement textElement = (TextElement)(nextObj ?? prevObj);

                if (textElement != null)
                {
                    tbBold.IsChecked = textElement.FontWeight == FontWeights.Bold;
                    tbItalic.IsChecked = textElement.FontStyle == FontStyles.Italic;
                    //...
                }
            }          
        }

此代码正在格式化选定的文本,当光标位于格式化字符/具有格式化字符之前时切换切换按钮的状态,就像在单词中一样...

1.)这样的代码在哪里属于?代码隐藏还是 ViewModel?实际上我知道答案,因为 RTBox 的技术限制它将保留在代码隐藏中。

2.)切换格式按钮的逻辑是通过以下方式完成的,

但这不起作用,因为 BoldCommand 无法执行格式化所选文本的方法,因为 RTB 的所选文本无法绑定到 ViewModel...

如何应对?

更新

ward Bell说:

“我对代码隐藏中的某些代码持开放态度;……我在决策逻辑上划清界限。当我看到任何类型的条件语句,这就是我们应该测试的代码中滋生错误的地方。”

来源: http:// codebetter.com/blogs/wardbell/archive/2010/03/19/mvvm-josh-smith-s-way.aspx

I really wonder what I can put in the ViewModel.

If its needed to be tested might some say...

But when the properties like CaretPosition (to get the selected Text) are no dependency

properties, then better forget about the ViewModel.

At the moment I have this in the codebehind:

private void rtbContent_SelectionChanged(object sender, System.Windows.RoutedEventArgs e)
        {
            RichTextBox rtb = sender as RichTextBox;

            TextPointer tpForward = rtb.CaretPosition.GetNextContextPosition(LogicalDirection.Forward);
            TextPointer tpBackward = rtb.CaretPosition.GetNextContextPosition(LogicalDirection.Backward);

            if (tpForward != null && tpBackward != null)
            {
                DependencyObject nextObj = tpForward.GetAdjacentElement(LogicalDirection.Forward);
                DependencyObject prevObj = tpBackward.GetAdjacentElement(LogicalDirection.Backward);

                TextElement textElement = (TextElement)(nextObj ?? prevObj);

                if (textElement != null)
                {
                    tbBold.IsChecked = textElement.FontWeight == FontWeights.Bold;
                    tbItalic.IsChecked = textElement.FontStyle == FontStyles.Italic;
                    //...
                }
            }          
        }

This code is formatting selected Text, toggles the state of the togglebutton when the cursor is before a formatted char/has formatted chars, just like in word...

1.) Where does such code belong? Code-behind or ViewModel? Actually I know the answer because of technical restrictions of the RTBox it will stay in the code-behind.

2.) The logic to toggle the format buttons is done via

But that will not work as the BoldCommand can not execute a method which would format the selected Text, as the selected Text of the RTB can not be bound to the ViewModel...

How to cope with that ?

UPDATE:

ward bell said:

"I am open to some code in the code-behind; ...I draw the line at decision logic. I smell a rat when I see a conditional statement of any kind. That’s where bugs breed. Conditional logic is code we should be testing."

source: http://codebetter.com/blogs/wardbell/archive/2010/03/19/mvvm-josh-smith-s-way.aspx

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

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

发布评论

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

评论(1

々眼睛长脚气 2024-09-19 15:49:53

数据进入模型,UI 进入视图,其中包括用于显示的格式化文本。现在,请记住,视图还可以包含隐藏代码——它不必是严格的 XAML。

Data goes in the Model, UI goes in the View, and that includes formatting text for display. Now, keep in mind the View can also include code-behind--it does not have to be strictly XAML.

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