用于静态数量组合框的 XAML 控制数组
我想要一个 wpf 表单上有 10 个组合框的数组。
组合框的 ItemsSource 是相同的 - 可选项目的 ObservableCollection。
每个选定的项目将绑定到不同 ObservableCollection 中的一个项目,想象中称为“SelectedItems”。
处理数组的最佳方法是什么?我当然可以有 10 个单独的组合框,但这不是很优雅。
我不认为 ItemsControl 模板是我想要的,因为组合框的数量是静态的。
谢谢
乔
I would like an array of 10 combo boxes on a wpf form.
The ItemsSource of the combo boxes are identical - an ObservableCollection of selectable Items.
Each Selected Item will bound to an item in a different ObservableCollection, imaginatively called 'SelectedItems'..
What is the best way to do the array? I could of course have 10 separate combo boxes but this would be not very elegant..
I don't think an ItemsControl template is what I'm after as the number of combo boxes is static.
Thanks
Joe
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果我理解正确的话,您有 10 个具有相同项目列表但不同数据源的 ComboBox
在这种情况下,我可以为 ComboBox 创建一个通用样式,它设置通用属性,例如
ItemsSource
(和SelectedItem
(如果所有项目的绑定都相同),然后根据需要在表单上实际创建单独的 ComboBox。当然,如果您的 ComboBox 的数据源可以放入集合中,那么最好将它们放入集合中,并且您使用
ItemsControl
来显示 ComboBoxIf I understand you right, you have 10 ComboBoxes with the same item list, but different data sources
In that case, I could create a common style for the ComboBox which sets the common properties such as
ItemsSource
(andSelectedItem
if the binding is the same for all items), and then actually create the individual ComboBoxes on the form as needed.Of course, if the DataSource for your ComboBoxes CAN be put in a collection, it is preferred that they are and that you use an
ItemsControl
to display the ComboBoxes假设您有效地绑定到一个集合,我将创建一个 ItemsControl 模板,并以这种方式对待它。除非您想自定义布局(即:这些不会在视图中排列在一起),否则即使项目数量始终是“静态”,这也会简化设计。
如果您希望将项目单独排列在视图上,那么只有 10 个组合框可能更合适。
Given that you're effectively binding to a collection, I would do an ItemsControl template, and just treat it that way. Unless you want to customize the layout (ie: these won't be arranged together in the View), that will simplify the design, even if the number of items is always "static".
If you want to have the items arranged separately on the View, then just having 10 combo boxes may be more appropriate.
就我个人而言,我认为
ItemsControl
具有一个ItemTemplate
来构造每个ComboBox
是正确的选择!您总是会恰好拥有其中 10 个吗?从 MVVM 的角度来看,我可以想象一个父视图模型,它有一组选择视图模型。每个选择视图模型都将具有可以选择的项目列表以及当前选定的项目。该视图模型将轻松绑定到您的视图。
Personally I think an
ItemsControl
which has anItemTemplate
which constructs eachComboBox
is the way to go! Are you always going to have exactly 10 of these?From an MVVM perspective, I can imagine a parent View Model which has a collection of selection view models. Each selection view model will have the list of items that can be selected, and the currently selected item. This view model will easily bind to your view.
不知道为什么你需要这个......这可能对决定如何做到这一点很重要......这是我的尝试。
为什么不设计 UI、为每个 ComboBox 命名、创建一个列表并在运行时将每个组合添加到该列表中?
Not knowing why you need this... which would probably be important in deciding how to do this... here is my stab at it.
Why not design the UI, give each ComboBox a name, create a List and Add each into that List at runtime?