C# 如何格式化ListView中RichEditBox的文本

C# 如何格式化ListView中RichEditBox的文本,c#,xaml,mvvm,C#,Xaml,Mvvm,我使用一个简单的MVVM模式将字符串数据绑定到RichEditBox RichEditBox位于ListView的数据模板中 <ListView.ItemTemplate> <DataTemplate x:DataType="local:Result"> <StackPanel Orientation="Vertical"> <Button Content="Bold" Click="but

我使用一个简单的MVVM模式将字符串数据绑定到RichEditBox

RichEditBox位于ListView的数据模板中

<ListView.ItemTemplate>
    <DataTemplate x:DataType="local:Result">
            <StackPanel Orientation="Vertical">
                <Button Content="Bold" Click="button1_Click"/>              
                        <local:RichEditBoxExtended RtfText="{x:Bind MyString, Mode=TwoWay}"/>
        </StackPanel>
    </DataTemplate>
</ListView.ItemTemplate>
正如可能注意到的,我使用的不是RichEditBox,而是从RichEditBox继承的控件。以下是本主题的主题,为了方便起见,包含了RichEditBoxExtended类

public class RichEditBoxExtended : RichEditBox
    {
        public static readonly DependencyProperty RtfTextProperty =
            DependencyProperty.Register(
            "RtfText", typeof(string), typeof(RichEditBoxExtended),
            new PropertyMetadata(default(string), RtfTextPropertyChanged));

        private bool _lockChangeExecution;

        public RichEditBoxExtended()
        {
            TextChanged += RichEditBoxExtended_TextChanged;
        }

        public string RtfText
        {
            get { return (string)GetValue(RtfTextProperty); }
            set { SetValue(RtfTextProperty, value); }
        }

        private void RichEditBoxExtended_TextChanged(object sender, RoutedEventArgs e)
        {
            if (!_lockChangeExecution)
            {
                _lockChangeExecution = true;
                string text;
                Document.GetText(TextGetOptions.None, out text);
                if (string.IsNullOrWhiteSpace(text))
                {
                    RtfText = "";
                }
                else
                {
                    Document.GetText(TextGetOptions.FormatRtf, out text);
                    RtfText = text;
                }
                _lockChangeExecution = false;
            }
        }

        private static void RtfTextPropertyChanged(DependencyObject dependencyObject,
            DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
        {
            var rtb = dependencyObject as RichEditBoxExtended;
            if (rtb == null) return;
            if (!rtb._lockChangeExecution)
            {
                rtb._lockChangeExecution = true;
                rtb.Document.SetText(TextSetOptions.FormatRtf, rtb.RtfText);
                rtb._lockChangeExecution = false;
            }
        }
    }

现在,我希望能够突出显示RichEditBoxExtended实例中的文本,然后通过单击DataTemplate中的粗体按钮将其变为粗体。这样的事情怎么可能实现呢

考虑改写你的问题,把重点放在问题本身上。希望现在问题更清楚了:)
public class RichEditBoxExtended : RichEditBox
    {
        public static readonly DependencyProperty RtfTextProperty =
            DependencyProperty.Register(
            "RtfText", typeof(string), typeof(RichEditBoxExtended),
            new PropertyMetadata(default(string), RtfTextPropertyChanged));

        private bool _lockChangeExecution;

        public RichEditBoxExtended()
        {
            TextChanged += RichEditBoxExtended_TextChanged;
        }

        public string RtfText
        {
            get { return (string)GetValue(RtfTextProperty); }
            set { SetValue(RtfTextProperty, value); }
        }

        private void RichEditBoxExtended_TextChanged(object sender, RoutedEventArgs e)
        {
            if (!_lockChangeExecution)
            {
                _lockChangeExecution = true;
                string text;
                Document.GetText(TextGetOptions.None, out text);
                if (string.IsNullOrWhiteSpace(text))
                {
                    RtfText = "";
                }
                else
                {
                    Document.GetText(TextGetOptions.FormatRtf, out text);
                    RtfText = text;
                }
                _lockChangeExecution = false;
            }
        }

        private static void RtfTextPropertyChanged(DependencyObject dependencyObject,
            DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs)
        {
            var rtb = dependencyObject as RichEditBoxExtended;
            if (rtb == null) return;
            if (!rtb._lockChangeExecution)
            {
                rtb._lockChangeExecution = true;
                rtb.Document.SetText(TextSetOptions.FormatRtf, rtb.RtfText);
                rtb._lockChangeExecution = false;
            }
        }
    }