I have implemented a basic data binding in code behind, this is the code:
Binding bindingSlider = new Binding();
bindingSlider.Source = mediaElement.Position;
bindingSlider.Mode = BindingMode.TwoWay;
bindingSlider.Converter = (IValueConverter)Application.Current.Resources["DoubleTimeSpan"];
slider.SetBinding(Slider.ValueProperty, bindingSlider);
And this is the converter's code,
class DoubleTimeSpan : IValueConverter
{
public object Convert(object value, Type targetType, object parameter,
string language)
{
return ((TimeSpan)value).TotalSeconds;
}
public object ConvertBack(object value, Type targetType, object parameter,
string language)
{
return TimeSpan.FromSeconds((double)value);
}
}
Even though I don't receive compiler's error message, but the binding code is not working. Why?
bindingSlider.Source = mediaElement.Position ; // boo!
This is wrong. Source
is the object containing the property you are binding to. What you want is
bindingSlider.Source = mediaElement ;
bindingSlider.Path = new PropertyPath ("Position") ;
You need to use the Path
property instead of Source
in data binding.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With