Reputation: 897
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?
Upvotes: 5
Views: 1386
Reputation: 21
You need to use the Path
property instead of Source
in data binding.
Upvotes: 2
Reputation: 5423
If the Source you are binding to is a UIElement
, try this :
Binding bindingSlider = new Binding("Position");
bindingSlider.ElementName = "mediaElement";
bindingSlider.Mode = BindingMode.TwoWay;
bindingSlider.Converter = (IValueConverter)Application.Current.Resources["DoubleTimeSpan"];
slider.SetBinding(Slider.ValueProperty, bindingSlider);
Upvotes: 0
Reputation: 20066
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") ;
Upvotes: 2
Reputation: 5421
It is a quite difficult to get from your code what is wrong. As Fabian said - check output windows. But increase binding trace level before. Check next to know how to do it.
PresentationTraceSources.TraceLevel
Upvotes: 0