way two raisepropertychanged property change silverlight binding textbox selection

silverlight - two - wpf binding textbox



¿Cómo vincular SelectionStart Property of Text Box? (4)

No puede vincularse a SelectionStart porque no es DependencyProperty.

Yo uso esto:

<TextBox x:Name="Test"/> <TextBlock Text="{Binding SelectionStart, ElementName=Test}"/>

pero siempre muestra 0.
¿Cómo puedo tratarlo?
Gracias.


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

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


Me encontré con este problema (SelectionStart y SelectionLength no son propiedades de dependencia) y decidí hacer un TextBox con inicio y fin de selección enlazables:

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


Esta 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>

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

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.