C # binding does not work

I did the basic data binding in the 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 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 if I do not receive a compiler error message, but the binding code does not work. Why?

+5
source share
4 answers
bindingSlider.Source = mediaElement.Position ; // boo!

It is not right. Sourceis an object containing the property to which you are bound. Do you want to

bindingSlider.Source = mediaElement ;
bindingSlider.Path   = new PropertyPath ("Position") ;
+2
source

To bind the data you need to use the property Pathinstead Source.

+2
source
0

, , UIElement, :

Binding bindingSlider = new Binding("Position");
bindingSlider.ElementName = "mediaElement";
bindingSlider.Mode = BindingMode.TwoWay;            
bindingSlider.Converter = (IValueConverter)Application.Current.Resources["DoubleTimeSpan"];            
slider.SetBinding(Slider.ValueProperty, bindingSlider);
0

All Articles