2010-08-02 4 views
-1

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

Répondre

0

J'ai mélangé un comportement attaché avec une propriété de dépendance, voici la solution:

Mettez un RichTextBox dans un UserControl et ce code trop :

 // SelectedText property. 
      public static readonly DependencyProperty SelectedTextProperty = 
       DependencyProperty.Register("SelectedText", typeof(TextSelection), 
       typeof(MyRichTextBox)); 

     /// <summary> 
      /// Default constructor. 
      /// </summary> 
      public MyRichTextBox() 
      { 
       InitializeComponent(); 

       this.TextBox.SelectionChanged += new RoutedEventHandler(TextBox_SelectionChanged); 
      } 

      void TextBox_SelectionChanged(object sender, RoutedEventArgs e) 
      { 
       var sT = (e.OriginalSource as RichTextBox).Selection; 
       SelectedText = sT; 
      } 




    /// <summary> 
    /// The WPF Selected Text of the FlowDocument in the control 
    /// </summary> 
    public TextSelection SelectedText 
    { 
     get { return (TextSelection)GetValue(SelectedTextProperty); } 
     set { SetValue(SelectedTextProperty, value); } 
    } 

    //UserControl embedded in the MainWindow.xaml 
     <My:MyRichTextBox SelectedText="{Binding SelectedDocument,Mode=TwoWay}" x:Name="EditBox" /> 

Vous avez maintenant accès à la TextSelection de la zone de texte riche dans votre ViewModel!

Questions connexes