Dear sir,
I use the RadNumericUpDown control in Silverlight and cannot update the Value property in my ViewModel via binding explicitly. First there is no event that tells me, when the text in the control changed. Second the reaction on KeyUp event and an explicit update of the BindigExpression.UpdateSource doesn’t work.
XAML:
<telerik:RadNumericUpDown VerticalAlignment="Center" Value="{Binding Dto.Laufzeit, Mode=TwoWay}" ShowButtons="False"
ValueFormat="Numeric" NumberDecimalDigits="0" Minimum="-9999" Maximum="99999" SmallChange="1" LargeChange="10" Margin="5,0,5,5"
Visibility="{Binding Dto.IsLaufzeitVisible, Converter={StaticResource boolToVisibilityConverter}}" IsEditable="{Binding Dto.IsLaufzeitEditable}"
Views:NumericUpDownBindingHelper.UpdateSourceOnChange="True">
<i:Interaction.Behaviors>
<Views:CldbControlStyleBehavior DefaultStyle="{StaticResource CldbNumericUpDownStyle}" ExtendedStyle="{StaticResource CldbNumericUpDownMustStyle}"
IsExtended="{Binding Path=Dto.IsLaufzeitRequired}"
IsExtendedEnabled="{Binding Dto.IsEditable}"/>
</i:Interaction.Behaviors>
</telerik:RadNumericUpDown>
The behavior:
public class NumericUpDownBindingHelper
{
public static bool GetUpdateSourceOnChange(DependencyObject obj)
{
return (bool)obj.GetValue(UpdateSourceOnChangeProperty);
}
public static void SetUpdateSourceOnChange(DependencyObject obj, bool value)
{
obj.SetValue(UpdateSourceOnChangeProperty, value);
}
public static readonly DependencyProperty UpdateSourceOnChangeProperty =
DependencyProperty.RegisterAttached("UpdateSourceOnChange", typeof(bool), typeof(NumericUpDownBindingHelper), new PropertyMetadata(false, OnPropertyChanged));
private static void OnPropertyChanged(DependencyObject obj, DependencyPropertyChangedEventArgs e)
{
var control = obj as RadNumericUpDown;
if (control == null)
return;
if ((bool)e.NewValue)
{
control.KeyUp += control_KeyUp;
control.ValueChanged += OnValueChanged;
}
else
{
control.KeyUp -= control_KeyUp;
control.ValueChanged -= OnValueChanged;
}
}
static void control_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
var control = sender as RadNumericUpDown;
if (control == null)
{
return;
}
var be = control.GetBindingExpression(RadNumericUpDown.ValueProperty);
if (be != null)
{
be.UpdateSource();
}
}
private static void OnValueChanged(object sender, RadRangeBaseValueChangedEventArgs e)
{
var control = sender as RadNumericUpDown;
if (control == null)
{
return;
}
var be = control.GetBindingExpression(RadNumericUpDown.ValueProperty);
if (be != null)
{
be.UpdateSource();
}
}
}
ViewModel:
private int? _laufzeit;
public int? Laufzeit
{
get { return _laufzeit; }
set
{
_laufzeit = value;
RaisePropertyChanged(() => Laufzeit);
Validate();
}
}
What should I do, to change the Value on every keyup event?
Best Regards