2009-07-24 8 views

Respuesta

0

Hasta donde yo sé, esta característica no se ha incluido en Silverlight 2.0.

Lea this artículo para una solución alternativa.

+0

Esta característica se ha añadido en Silverlight 3. y funciona muy bien en este ejemplo: Pero no funciona en el mío. No me gusta ese artículo, este estilo en realidad mata toda la belleza de la unión. – Ivan

1

No se puede vincular a SelectionStart porque no es DependencyProperty.

+2

:) Muchas gracias. – Ivan

+0

¿Hay alguna forma de averiguar qué propiedades en un control determinado son DependencyProperties y cuáles no? –

+0

La manera más rápida es usar Intellisense of Visual Studio. Por ejemplo, suponga que desea ver todas las propiedades de dependencia de un cuadro de texto. Simplemente escriba TextBox. e intellisense le mostrará todas sus propiedades de dependencia. –

9

me encontré con este problema (SelectionStart y SelectionLength no son propiedades de dependencia) y decidió hacer un cuadro de texto con el inicio de la selección enlazable y al final:

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

¡Buen hombre, muy útil! –

0

Esto podría ser una solución alternativa:

Ver :

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

modelo de vista:

#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 

Estoy de acuerdo en que tiene que copiar el tipo de componente TextBox en ViewModel y es un tipo de acoplamiento, pero crear un componente personalizado impondrá también vincular una propiedad específica.