2009-07-24 47 views

Trả lời

0

Theo như tôi biết, tính năng này chưa được bao gồm trong Silverlight 2.0.

Đọc this bài viết cho giải pháp xung quanh.

+0

Tính năng này đã được thêm vào trong Silverlight 3. và hoạt động rất tốt trong ví dụ này: Nhưng không hoạt động trong mỏ. Tôi không thích bài viết đó, phong cách này thực sự giết chết tất cả vẻ đẹp của ràng buộc. – Ivan

1

Bạn không thể liên kết với SelectionStart vì nó không phải là DependencyProperty.

+2

:) Cảm ơn bạn rất nhiều. – Ivan

+0

Có cách nào để tìm ra các thuộc tính nào trên một điều khiển đã cho là DependencyProperties và các thuộc tính nào không? –

+0

Cách nhanh nhất là sử dụng Intellisense của Visual Studio. Ví dụ: Giả sử bạn muốn xem tất cả Thuộc tính phụ thuộc của một Hộp văn bản. Chỉ cần gõ TextBox. và intellisense sẽ cho bạn thấy tất cả các thuộc tính phụ thuộc của nó. –

9

Tôi chạy vào vấn đề này (SelectionStart và SelectionLength không tính phụ thuộc) và quyết định làm một TextBox với đầu lựa chọn bindable và kết thúc:

public class SelectionBindingTextBox : TextBox 
{ 
    public static readonly DependencyProperty BindableSelectionStartProperty = 
     DependencyProperty.Register(
     "BindableSelectionStart", 
     typeof(int), 
     typeof(SelectionBindingTextBox), 
     new PropertyMetadata(OnBindableSelectionStartChanged)); 

    public static readonly DependencyProperty BindableSelectionLengthProperty = 
     DependencyProperty.Register(
     "BindableSelectionLength", 
     typeof(int), 
     typeof(SelectionBindingTextBox), 
     new PropertyMetadata(OnBindableSelectionLengthChanged)); 

    private bool changeFromUI; 

    public SelectionBindingTextBox() : base() 
    { 
     this.SelectionChanged += this.OnSelectionChanged; 
    } 

    public int BindableSelectionStart 
    { 
     get 
     { 
      return (int)this.GetValue(BindableSelectionStartProperty); 
     } 

     set 
     { 
      this.SetValue(BindableSelectionStartProperty, value); 
     } 
    } 

    public int BindableSelectionLength 
    { 
     get 
     { 
      return (int)this.GetValue(BindableSelectionLengthProperty); 
     } 

     set 
     { 
      this.SetValue(BindableSelectionLengthProperty, value); 
     } 
    } 

    private static void OnBindableSelectionStartChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args) 
    { 
     var textBox = dependencyObject as SelectionBindingTextBox; 

     if (!textBox.changeFromUI) 
     { 
      int newValue = (int)args.NewValue; 
      textBox.SelectionStart = newValue; 
     } 
     else 
     { 
      textBox.changeFromUI = false; 
     } 
    } 

    private static void OnBindableSelectionLengthChanged(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args) 
    { 
     var textBox = dependencyObject as SelectionBindingTextBox; 

     if (!textBox.changeFromUI) 
     { 
      int newValue = (int)args.NewValue; 
      textBox.SelectionLength = newValue; 
     } 
     else 
     { 
      textBox.changeFromUI = false; 
     } 
    } 

    private void OnSelectionChanged(object sender, RoutedEventArgs e) 
    { 
     if (this.BindableSelectionStart != this.SelectionStart) 
     { 
      this.changeFromUI = true; 
      this.BindableSelectionStart = this.SelectionStart; 
     } 

     if (this.BindableSelectionLength != this.SelectionLength) 
     { 
      this.changeFromUI = true; 
      this.BindableSelectionLength = this.SelectionLength; 
     } 
    } 
} 
+0

Người đàn ông tốt, rất hữu ích! –

0

Đây có thể là một giải pháp thay thế:

Xem :

<TextBox Text="{Binding Text}"> 
    <i:Interaction.Triggers> 
     <i:EventTrigger EventName="SelectionChanged"> 
      <mvvml:EventToCommand Command="{Binding TextBoxSelectionChangedCommand}" 
           PassEventArgsToCommand="True" /> 
     </i:EventTrigger> 
    </i:Interaction.Triggers> 
</TextBox> 

ViewModel:

#region TextBoxSelectionChangedCommand 
    RelayCommand<RoutedEventArgs> _TextBoxSelectionChangedCommand = null; 
    public ICommand TextBoxSelectionChangedCommand { 
     get { 
      if (_TextBoxSelectionChangedCommand == null) { 
       _TextBoxSelectionChangedCommand = new RelayCommand<RoutedEventArgs>((r) => TextBoxSelectionChanged(r), (r) => true); 
      } 

      return _TextBoxSelectionChangedCommand; 
     } 
    } 

    protected virtual void TextBoxSelectionChanged(RoutedEventArgs _args) { 
     YourCursorPositionVariable = (_args.OriginalSource as System.Windows.Controls.TextBox).SelectionStart; 
    } 
    #endregion 

Tôi đồng ý bạn phải truyền loại thành phần TextBox trong ViewModel và đó là một loại khớp nối, nhưng tạo thành phần tùy chỉnh sẽ áp đặt để ràng buộc trên một thuộc tính cụ thể.

Các vấn đề liên quan