2017-03-01 6 views
0

WPF。ToggleButton IsCheckedプロパティをRichTextBoxのアタッチされた動作にバインドする構文

私は行動を持っている:

としてリッチテキストボックスに取り付けられている。
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 x:Name="RichTextControl" ... 
          > 
         <i:Interaction.Behaviors> 
          <b:RichTextBehavior TextFont="{Binding ElementName=Fonttype, Path=SelectedItem, Mode=TwoWay}"/> 
         </i:Interaction.Behaviors> 

        </RichTextBox> 

私は行動の「TextBold」プロパティにトグルボタンにisCheckedプロパティをバインドしたいと思います( XAMLで)、などの何か:

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

これはどのように行われますか?構文は何ですか? どんな考えでも大歓迎です。 TIA

編集:

私は、これは動作するはずと信じて、それはしません。実際には、ボールドトグルボタンを押すと、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}"........ 

私が追加される場合がありますが、何らかの理由で、リッチテキストボックス内のすべてのキーを押して、行動にSel​​ectionChangedを呼び出しているようです(私が期待していたほどではない)、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

はあなたが行動にSelectionChangedイベントを処理することができます:

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

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

GetPropertyValueを使用することにより、propertly 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; 
    } 

+0

私はチェックボックスの使用を検討しませんでした。 ToggleButtonに問題はありますか?私の編集を見てください。ありがとう。 –

+0

ToggleButtonでも使用できます。あなたはそれを使用して問題がありましたか? – Ron

+0

私は多くのことを学びます。どうやら、ToggleButton/CheckBoxのIsCheckedを使ってSelectionChangedをもう一度呼び出すことや、ToggleButtonからのバインディングを指定した方法がRichTextBoxの動作を参照している可能性があります。どちらの場合でも、ToggleButtonからIsCheckedバインディングを削除して、ソリューションを使用すると完全に機能します。ありがとう。 :) –

2

は、たぶんこれが何をしたいです双方向バインディングを使用する(私はを使用する):

<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> 
+0

実際にはそれ以外の方法で探しています。つまり、ターゲットはToggleButtonです。ありがとう。 –

+0

申し訳ありませんが、私はあなたの意味を理解していない、これはあなたが欲しいものです:バインディングにMode = OneWayToSourceを追加しますか? – WPInfo

+0

試してみてください。運がない。 –

関連する問題