essayer mon premier comportement attaché: Je veux lier le TextSelection du RichTextBox à mon ViewModel`s propriété:WPF: Le comportement joint est enregistré mais jamais appelé?
public TextSelection SelectedRichText {get;set;}
De cette façon je lier:
<RichTextBox behavior:RichTextBoxSelectionBehavior.RichTextBoxSelection="{Binding SelectedRichText}" />
Cest mon code et je 2 questions:
1) Pourquoi OnRichTextBoxSelectionPropertyChanged n'est jamais appelé? 2) voir la question est cette méthode en bas: OnRichTextBoxGotSelectedText
public static class RichTextBoxSelectionBehavior
{
public static TextSelection GetRichTextBoxSelection(DependencyObject obj)
{
return (TextSelection)obj.GetValue(RichTextBoxSelection);
}
public static void SetRichTextBoxSelection(DependencyObject obj, TextSelection value)
{
obj.SetValue(RichTextBoxSelection, value);
}
// Using a DependencyProperty as the backing store for MyProperty.
public static readonly DependencyProperty RichTextBoxSelection =
DependencyProperty.RegisterAttached
(
"RichTextBoxSelection",
typeof(TextSelection),
typeof(RichTextBoxSelectionBehavior),
new UIPropertyMetadata(OnRichTextBoxSelectionPropertyChanged)
);
private static void OnRichTextBoxSelectionPropertyChanged(DependencyObject dpo, DependencyPropertyChangedEventArgs args)
{
RichTextBox rtb = dpo as RichTextBox;
if (rtb != null)
{
if (!((TextSelection)args.NewValue).IsEmpty)
{
// if the TextSelected has selected text hook up the RichTextBox intenal SelectedChanged event with my own
rtb.SelectionChanged += OnRichTextBoxGotSelectedText;
}
else
{
rtb.SelectionChanged -= OnRichTextBoxGotSelectedText;
}
}
}
private static void OnRichTextBoxGotSelectedText(object sender, RoutedEventArgs e)
{
RichTextBox rtb = (RichTextBox) sender;
// How can I pass now my rtb.Selection to the property the behavior is bound to? e.g. my SelectedRichText property in the ViewModel
//Action action =() => { rtb.Selection; };
//rtb.Dispatcher.BeginInvoke(action, DispatcherPriority.ContextIdle);
}
}