代码之家  ›  专栏  ›  技术社区  ›  msfanboy

WPF:附加行为已注册但从未调用?

  •  -1
  • msfanboy  · 技术社区  · 16 年前

    尝试我的第一个附加行为:我想将RichTextBox的TextSelection绑定到我的ViewModel属性:

    public TextSelection SelectedRichText {get;set;}
    

    这样我就把它绑起来了:

    <RichTextBox behavior:RichTextBoxSelectionBehavior.RichTextBoxSelection="{Binding SelectedRichText}" />
    

    这是我的代码,我有两个问题:

    1) 为什么从未调用OnRichTextBoxSelectionPropertyChanged?

    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);
            }
        }
    
    1 回复  |  直到 16 年前
        1
  •  0
  •   msfanboy    16 年前

    将RichTExtBox放在UserControl中,并使用以下代码:

         // 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" />
    

    现在您可以访问ViewModel中richtextbox的TextSelection了!