【发布时间】:2016-03-17 23:27:32
【问题描述】:
我正在开发一个媒体应用程序,我有一个设置窗口,其中有一个带有 3 个组合框的堆栈面板。这些组合框的 ItemSource 都绑定到同一个 List(Of SoundDevice) 以向用户显示所有可用的声音计算机上的设备。
这些组合框 SelectedValue 中的每一个都绑定到我的 SettingsClass 对象的子类(称为 SoundDevicesClass)上的不同公共属性,允许用户为不同的任务选择不同的设备。
我现在想在绑定到我的 SoundDevicesClass 的另一个实例的应用程序的另一个区域中重新使用这个相同的堆栈面板。
我的计划是将堆栈面板移动到 DataTemplate 中,然后将其放在我的设置窗口上的 ContentControl 中。然后可以通过
轻松将其绑定到后端对象Content="{Binding Settings.SoundDevices}".
我的问题是我应该将组合框 ItemsSource 绑定到什么?在设置窗口中,它使用 {binding ElementName=SettingsWindow,Path=AllowedSoundDevices} 绑定到设置窗口的一个名为 AllowedSoundDevices 的属性,该属性工作正常,但如果我将数据模板移动到共享资源字典显然将无法工作。
我的第一个想法是定义一个名为 SoundDevicesSettingsClass 之类的新类,例如:
Public Class SoundDevicesSettingsClass
Public Property AllowedSoundDevices as AllowedSoundDevicesClass
Public Property SoundDevices as SoundDevicesClass
End Class
然后将 Content 绑定设置为该类的实例。
这是最好的方法还是我可以遵循更好的做法?
感谢所有帮助!
【问题讨论】:
标签: wpf vb.net binding datatemplate contentcontrol