C# 绑定(bind)不起作用

标签 c# wpf data-binding

我已经在代码后面实现了一个基本的数据绑定(bind),这是代码:

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);
    }
}

尽管我没有收到编译器的错误消息,但绑定(bind)代码无法正常工作。为什么?

最佳答案

bindingSlider.Source = mediaElement.Position ; // boo!

这是错误的。 Source 是包含您要绑定(bind)到的属性的对象。你想要的是

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

关于C# 绑定(bind)不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14750566/

相关文章:

objective-c - 在不相关的(表) View Controller 之间交换数据?

c# - CSharp 引用在 Delphi XE2 中编译的 x64 OCX

c# - 如果 GetOrAdd 正忙于使用相同的键检索值,它会等待吗?

c# - Web 浏览器控件 - 检测何时调用 js window.close

c# - 引用库中的 ASP.NET 相对路径

c# - 删除 "/*"和 "*/"之间的文本( block 注释)

wpf - wpf MVVM中的多个DataContext?

wpf - 如果第一个属性是 "undefined",则绑定(bind)到第二个属性

wpf - wpf 中包含静态和动态数据的组合框

mysql - 如何将数据表绑定(bind)到 reportviewer 运行时