将 ToggleButton IsChecked 属性 绑定到 RichTextBox 的附加行为的语法

Syntax to bind a ToggleButton IsChecked property to a RichTextBox's attached behavior

WPF.

我有一个行为:

public class RichTextBehavior : Behavior<RichTextBox>
    {
         public bool TextBold
        {
            get { return (bool)GetValue(TextBoldProperty); }
            set { SetValue(TextBoldProperty, value); }
        }

        // Using a DependencyProperty as the backing store for TextBold.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TextBoldProperty =
            DependencyProperty.Register("TextBold", typeof(bool), typeof(RichTextBehavior),
             new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnTextBoldChanged));

        private static void OnTextBoldChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var behavior = d as RichTextBehavior;
            TextRange tr = new TextRange(behavior.AssociatedObject.Selection.Start, behavior.AssociatedObject.Selection.End);
            if (tr == null)
                return;

            // This Works, but also adds keyboard commands.
            //EditingCommands.ToggleBold.Execute(null, behavior.AssociatedObject);

            if ((bool)e.NewValue)
                //TextSelection ts = richTextBox.Selection;

                tr.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Bold);
            else
                tr.ApplyPropertyValue(TextElement.FontWeightProperty, FontWeights.Normal);

        }

附加到 RichTextBox 为:

 <RichTextBox x:Name="RichTextControl" ...
                            >
                        <i:Interaction.Behaviors>
                            <b:RichTextBehavior TextFont="{Binding ElementName=Fonttype, Path=SelectedItem, Mode=TwoWay}"/>
                        </i:Interaction.Behaviors>

                    </RichTextBox>

我想将 ToggleButton IsChecked 属性 绑定到行为的 'TextBold' 属性(在 XAML 中),某事喜欢:

<ToggleButton Name="ToggleBold"  Command="EditingCommands.ToggleBold" 
       CommandTarget="{Binding ElementName=RichTextControl}"
        IsChecked="{Binding ElementName=RichTextControl, Path=(TextBold), Mode=TwoWay}"
        .......................................           
 </ToggleButton>

这是怎么做到的?语法是什么? 非常感谢任何想法。 TIA

编辑:

我相信这应该有效,但它没有。实际上,按下 Bold Toggle 按钮确实会更改 RichTextBox 上键入的文本(使用 EditingCommands.ToggleBold 时应该如此),但和以前一样,选择 RichTextBox 中已有的文本不会更改 ToggleBold 绑定到的状态IsChecked 属性.

我相信这应该有效(但似乎不尊重 IsChecked 绑定):

 <ToggleButton Name="ToggleBold"  
Command="EditingCommands.ToggleBold" CommandTarget="{Binding ElementName=RichTextControl}"
    IsChecked="{Binding ElementName=RichTextControl, 
 Path=(b:RichTextBehavior.TextBold), Mode=TwoWay}"........

出于某种原因,我可能会补充说,RichTextBox 中的每个按键似乎都会在行为中调用 SelectionChanged 两次(而不是我期望的一次),并且第二次调用似乎没有已经设置的第一次通话的价值。奇怪。

也许这就是你想要的:

     <RichTextBox x:Name="RichTextControl">
        <i:Interaction.Behaviors>
            <b:RichTextBehavior TextBold="{Binding ElementName=toggleBold,Path=IsChecked}" />
        </i:Interaction.Behaviors>
    </RichTextBox>
    <ToggleButton x:Name="toggleBold" Content="Bold" />

您可以在行为中处理 SelectionChanged 事件:

    protected override void OnAttached()
    {
        base.OnAttached();
        AssociatedObject.SelectionChanged += RichTextBoxSelectionChanged;
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();
        AssociatedObject.SelectionChanged -= RichTextBoxSelectionChanged;
    }

并正确设置 TextBold,使用 GetPropertyValue:

    void RichTextBoxSelectionChanged(object sender, System.Windows.RoutedEventArgs e)
    {
        TextRange tr = new TextRange(AssociatedObject.Selection.Start, AssociatedObject.Selection.End);
        if (tr == null)
            return;
        FontWeight g = (FontWeight)tr.GetPropertyValue(TextElement.FontWeightProperty); 
        TextBold = g == FontWeights.Bold;
    }  

使用双向绑定(我使用CheckBox):

<StackPanel> 
    <RichTextBox x:Name="RichTextControl" >
        <i:Interaction.Behaviors>
            <local:RichTextBehavior TextBold="{Binding ElementName=fonttype, Path=IsChecked, Mode=TwoWay}"/>
        </i:Interaction.Behaviors> 
    </RichTextBox>
    <CheckBox x:Name="fonttype"  Content="Is Bold"   /> 
</StackPanel>