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

时间:2017-03-01 00:37:25

标签: wpf xaml attachedbehaviors

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属性绑定到&#39; TextBold&#39;行为的属性(在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中已有的文本并不会在绑定到IsChecked时更改ToggleBold的状态属性。

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

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

我可能会补充一点,出于某种原因,RichTextBox中的每个按键似乎都会在行为中调用SelectionChanged两次(不是我想象的那样),而第二次调用似乎没有已经设置的值第一个电话。怪异。

2 个答案:

答案 0 :(得分:2)

也许这就是你想要的:

     <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" />

答案 1 :(得分:1)

您可以在行为中处理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>