简体   繁体   English

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

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

WPF. WPF。

I have a behavior: 我有一个行为:

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);

        }

That is attached to a RichTextBox as: 附加到RichTextBox的方式如下:

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

                    </RichTextBox>

I would like to bind a ToggleButton IsChecked property to the 'TextBold' property of the behavior (in XAML), something like: 我想一个切换按钮物业器isChecked绑定的行为(在XAML)的“TextBold”属性, 这样

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

How is this done? 怎么做? What is the syntax? 语法是什么? Any idea is much appreciated. 任何想法都非常感谢。 TIA TIA

Edit: 编辑:

I believe this should work, but it doesn't. 我认为这应该可以,但是不能。 In action, depressing the Bold Toggle button DOES change the typed text on the RichTextBox (as it should with the EditingCommands.ToggleBold), but as before, selecting text already in the RichTextBox does NOT change the state of the ToggleBold when bound to the IsChecked property. 实际上,按下Bold Toggle按钮不会更改RichTextBox上的键入文本(就像使用EditingCommands.ToggleBold一样),但是像以前一样,选择RichTextBox中已经存在的文本绑定到IsChecked时不会更改ToggleBold的状态。属性。

I believe this should have worked ( but does not seem to respect the IsChecked Binding): 我相信这应该起作用(但似乎不尊重IsChecked绑定):

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

I might add, for some reason, every key press in the RichTextBox seems to call SelectionChanged in the behavior twice, (not once as I would expect), and the second call does not seem to have the already-set value of the first call. 由于某种原因,我可能会添加,RichTextBox中的每个按键似乎都在行为中两次调用SelectionChanged(而不是我期望的一次),并且第二个调用似乎没有第一个调用的已设置值。 Weird. 奇怪的。

Maybe this is what you want: 也许这就是您想要的:

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

You can handle SelectionChanged event in the Behavior: 您可以在“行为”中处理SelectionChanged事件:

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

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

and set TextBold propertly, by using GetPropertyValue : 并通过使用GetPropertyValue TextBold设置TextBold

    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;
    }  

Use two way binding (I use CheckBox ): 使用两种方式绑定(我使用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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM