2009-07-24 9 views

risposta

0

Per quanto ne so, questa funzione non è stata inclusa in Silverlight 2.0.

Leggi articolo this per una soluzione pratica.

+0

Questa caratteristica era stato aggiunto in Silverlight 3. e funziona molto bene in questo esempio: Ma non funziona nel mio. Non mi piace quell'articolo, questo stile uccide davvero tutta la bellezza del legame. – Ivan

1

Non è possibile eseguire il binding a SelectionStart perché non è una proprietà di dipendenza.

+2

:) Grazie mille. – Ivan

+0

C'è un modo per scoprire quali proprietà su un determinato controllo sono DependencyProperties e quali no? –

+0

Il modo più veloce è utilizzare Intellisense di Visual Studio. Ad esempio, si supponga di voler vedere tutte le proprietà dipendenze di un controllo TextBox. Basta digitare TextBox. e intellisense ti mostrerà tutte le sue proprietà di dipendenza. –

9

mi sono imbattuto in questo problema (SelectionStart e SelectionLength non sono proprietà di dipendenza) e ha deciso di fare una TextBox con associabile iniziale di una selezione e di fine:

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

Buon uomo, molto utile! –

0

Questa potrebbe essere una soluzione alternativa:

View :

<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 

Sono d'accordo che devi lanciare il tipo di componente TextBox in ViewModel ed è un tipo di accoppiamento, ma creare un componente personalizzato imporrà anche il binding su una proprietà specifica.

Problemi correlati