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}"........
私が追加される場合がありますが、何らかの理由で、リッチテキストボックス内のすべてのキーを押して、行動にSelectionChangedを呼び出しているようです(私が期待していたほどではない)、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" />
私はチェックボックスの使用を検討しませんでした。 ToggleButtonに問題はありますか?私の編集を見てください。ありがとう。 –
ToggleButtonでも使用できます。あなたはそれを使用して問題がありましたか? – Ron
私は多くのことを学びます。どうやら、ToggleButton/CheckBoxのIsCheckedを使ってSelectionChangedをもう一度呼び出すことや、ToggleButtonからのバインディングを指定した方法がRichTextBoxの動作を参照している可能性があります。どちらの場合でも、ToggleButtonからIsCheckedバインディングを削除して、ソリューションを使用すると完全に機能します。ありがとう。 :) –