簡體   English   中英

將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>

我想一個切換按鈕物業器isChecked綁定的行為(在XAML)的“TextBold”屬性, 這樣

<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綁定):

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

並通過使用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;
    }  

使用兩種方式綁定(我使用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