utilizo este:Cómo enlazar SelectionStart Property of Text Box?
<TextBox x:Name="Test"/>
<TextBlock Text="{Binding SelectionStart, ElementName=Test}"/>
pero siempre muestra 0.
¿Cómo es el tratamiento?
Gracias.
utilizo este:Cómo enlazar SelectionStart Property of Text Box?
<TextBox x:Name="Test"/>
<TextBlock Text="{Binding SelectionStart, ElementName=Test}"/>
pero siempre muestra 0.
¿Cómo es el tratamiento?
Gracias.
Hasta donde yo sé, esta característica no se ha incluido en Silverlight 2.0.
Lea this artículo para una solución alternativa.
No se puede vincular a SelectionStart porque no es DependencyProperty.
:) Muchas gracias. – Ivan
¿Hay alguna forma de averiguar qué propiedades en un control determinado son DependencyProperties y cuáles no? –
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. –
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;
}
}
}
¡Buen hombre, muy útil! –
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.
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