Io uso questo:Come associare la proprietà SelectionStart della casella di testo?
<TextBox x:Name="Test"/>
<TextBlock Text="{Binding SelectionStart, ElementName=Test}"/>
ma mostra sempre 0.
Come posso trattarlo?
Grazie.
Io uso questo:Come associare la proprietà SelectionStart della casella di testo?
<TextBox x:Name="Test"/>
<TextBlock Text="{Binding SelectionStart, ElementName=Test}"/>
ma mostra sempre 0.
Come posso trattarlo?
Grazie.
Per quanto ne so, questa funzione non è stata inclusa in Silverlight 2.0.
Leggi articolo this per una soluzione pratica.
Non è possibile eseguire il binding a SelectionStart perché non è una proprietà di dipendenza.
:) Grazie mille. – Ivan
C'è un modo per scoprire quali proprietà su un determinato controllo sono DependencyProperties e quali no? –
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. –
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;
}
}
}
Buon uomo, molto utile! –
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.
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