Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# binding not working

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?

like image 920
Herks Avatar asked Oct 05 '22 03:10

Herks


2 Answers

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") ;
like image 108
Anton Tykhyy Avatar answered Oct 07 '22 16:10

Anton Tykhyy


You need to use the Path property instead of Source in data binding.

like image 34
user2122394 Avatar answered Oct 07 '22 15:10

user2122394