WPF 使用 DependencyProperties 绑定 Slider Value 和 MaxValue
首先,我假设 DependencyProperty 是要使用的,但我可能是错的 - 也许 INotifyPropertyChanged 更合适?
我有一些与音频播放相关的 UI 控件。我想将音量和轨道搜索滑块绑定到几个双精度滑块。
- 更新值时,音量滑块将更新音频类 Volume 变量。
- Track Seeking 滑块 MaxValue 将设置为曲目的持续时间(以毫秒为单位),并将更新我的音频播放类的搜索位置变量
New to Dependency property 等非常感谢您的帮助!
Firstly I am assuming DependencyProperty is what to use but I could be wrong - Maybe a INotifyPropertyChanged is more suitable?
I have a few UI controls that are all associated to audio playback. I would like to bind the Volume and Track Seek Sliders to a couple of doubles.
- The volume slider will update the audio classes Volume variable when the Value is updated
- The Track Seeking slider MaxValue will be set to the duration of the track in milliseconds and will update the seeking position variable of my audio playback class
New to Dependency properties and such so you help is much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您不需要依赖属性,只需实现
INotifyPropertyChanged
因此绑定引擎知道您的属性的更改。You don't need dependency properties for that, simply implement
INotifyPropertyChanged
so the binding engine is aware of changes to your property.DependencyProperties 适用于稀疏数据结构,例如具有大量数据的控件仅设置少数属性,这可以节省内存。如果您绑定到自己的数据 DP,通常没有多大意义,因为您的属性将被设置,并且您可能希望从不同的线程访问您的数据对象,而这对于 DependencyObjects 来说是不容易实现的。
另请参阅此问题,它比较了两者在 ViewModel 中使用的情况。
DependencyProperties are for sparse data-structures like Controls which have a huge amount of properties of which only a few are set, this saves memory. If you bind to your own data DPs normally do not make that much sense because your properties will be set and you might want to access your data-object from different threads which is not easily possible with DependencyObjects.
Also see this question which compares the two in regards to using them in a ViewModel.