我在后面的代码中实现了一个基本的数据绑定,代码如下:
Binding bindingSlider = new Binding();
bindingSlider.Source = mediaElement.Position;
bindingSlider.Mode = BindingMode.TwoWay;
bindingSlider.Converter = (IValueConverter)Application.Current.Resources["DoubleTimeSpan"];
slider.SetBinding(Slider.ValueProperty, bindingSlider);
这是转换器的代码,
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);
}
}
即使我没有收到编译器的错误消息,但绑定代码不起作用。为什么?