2009-07-24 1 views

ответ

0

Насколько мне известно, эта функция не была включена в Silverlight 2.0.

Прочитать this статью для решения проблемы.

+0

Эта функция была добавлена ​​в Silverlight 3. и работает очень хорошо в этом примере: Но не работает в моей. Мне не нравится эта статья, этот стиль на самом деле убивает всю красоту привязки. – Ivan

1

Вы не можете привязываться к SelectionStart, потому что это не DependencyProperty.

+2

:) Большое спасибо. – Ivan

+0

Есть ли способ узнать, какие свойства для данного элемента управления являются DependencyProperties, а какие нет? –

+0

Самый быстрый способ - использовать Intellisense Visual Studio. Например, предположим, что вы хотите увидеть все свойства зависимостей TextBox. Просто введите TextBox. и intellisense покажет вам все свои свойства зависимости. –

9

Я столкнулся с этой проблемой (SelectionStart и SelectionLength не являются свойствами зависимостей) и решил сделать TextBox с привязываемому началом выбора и концом:

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

Хороший человек, очень полезный! –

0

Это может быть альтернативным решением:

Просмотра :

<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 

Я согласен, что вы должны использовать тип компонента TextBox в ViewModel, и это своего рода связывание, но создать пользовательский компонент будет навязываться для привязки и к определенному свойству.