用于静态数量组合框的 XAML 控制数组

发布于 2024-12-03 09:03:32 字数 282 浏览 1 评论 0原文

我想要一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

北恋 2024-12-10 09:03:32

如果我理解正确的话,您有 10 个具有相同项目列表但不同数据源的 ComboBox

在这种情况下,我可以为 ComboBox 创建一个通用样式,它设置通用属性,例如 ItemsSource (和SelectedItem(如果所有项目的绑定都相同),然后根据需要在表单上实际创建单独的 ComboBox。

<StackPanel>
    <StackPanel.Resources>
        <Style TargetType="{x:Type ComboBox}">
            <!-- Set the binding to wherever your ItemsSource resides. In this
                 case,I'm binding to a static class called Lists and a static 
                 property called ComboBoxItems -->
            <Setter Property="ItemsSource" 
                    Value="{Binding Source={x:Static local:Lists.ComboBoxItems}}" />

            <!-- Only use this setter if your binding is the same everywhere -->
            <Setter Property="SelectedItem" Value="{Binding SelectedItem}" />
        </Style>
    </StackPanel.Resources>

    <ComboBox DataContext="{Binding Item1}" />
    <ComboBox DataContext="{Binding Item2}" />
    <ComboBox DataContext="{Binding Item3}" />
    <ComboBox DataContext="{Binding Item4}" />
    <ComboBox DataContext="{Binding Item5}" />
    <ComboBox DataContext="{Binding Item6}" />
    <ComboBox DataContext="{Binding Item7}" />
    <ComboBox DataContext="{Binding Item8}" />
    <ComboBox DataContext="{Binding Item9}" />
    <ComboBox DataContext="{Binding Item10}" />
</StackPanel>

当然,如果您的 ComboBox 的数据源可以放入集合中,那么最好将它们放入集合中,并且您使用 ItemsControl 来显示 ComboBox

<ItemsControl ItemsSource="{Binding SelectedItems}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <ComboBox SelectedItem="{Binding }"
                      ItemsSource="{Binding Source={x:Static local:Lists.ComboBoxItems}}" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

If 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 (and SelectedItem if the binding is the same for all items), and then actually create the individual ComboBoxes on the form as needed.

<StackPanel>
    <StackPanel.Resources>
        <Style TargetType="{x:Type ComboBox}">
            <!-- Set the binding to wherever your ItemsSource resides. In this
                 case,I'm binding to a static class called Lists and a static 
                 property called ComboBoxItems -->
            <Setter Property="ItemsSource" 
                    Value="{Binding Source={x:Static local:Lists.ComboBoxItems}}" />

            <!-- Only use this setter if your binding is the same everywhere -->
            <Setter Property="SelectedItem" Value="{Binding SelectedItem}" />
        </Style>
    </StackPanel.Resources>

    <ComboBox DataContext="{Binding Item1}" />
    <ComboBox DataContext="{Binding Item2}" />
    <ComboBox DataContext="{Binding Item3}" />
    <ComboBox DataContext="{Binding Item4}" />
    <ComboBox DataContext="{Binding Item5}" />
    <ComboBox DataContext="{Binding Item6}" />
    <ComboBox DataContext="{Binding Item7}" />
    <ComboBox DataContext="{Binding Item8}" />
    <ComboBox DataContext="{Binding Item9}" />
    <ComboBox DataContext="{Binding Item10}" />
</StackPanel>

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 ItemsSource="{Binding SelectedItems}">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <ComboBox SelectedItem="{Binding }"
                      ItemsSource="{Binding Source={x:Static local:Lists.ComboBoxItems}}" />
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>
美煞众生 2024-12-10 09:03:32

每个选定的项目将绑定到不同 ObservableCollection 中的一个项目,想象中称为“SelectedItems”..

假设您有效地绑定到一个集合,我将创建一个 ItemsControl 模板,并以这种方式对待它。除非您想自定义布局(即:这些不会在视图中排列在一起),否则即使项目数量始终是“静态”,这也会简化设计。

如果您希望将项目单独排列在视图上,那么只有 10 个组合框可能更合适。

Each Selected Item will bound to an item in a different ObservableCollection, imaginatively called 'SelectedItems'..

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.

老旧海报 2024-12-10 09:03:32

就我个人而言,我认为 ItemsControl 具有一个 ItemTemplate 来构造每个 ComboBox 是正确的选择!您总是会恰好拥有其中 10 个吗?

从 MVVM 的角度来看,我可以想象一个父视图模型,它有一组选择视图模型。每个选择视图模型都将具有可以选择的项目列表以及当前选定的项目。该视图模型将轻松绑定到您的视图。

Personally I think an ItemsControl which has an ItemTemplate which constructs each ComboBox 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.

嗫嚅 2024-12-10 09:03:32

不知道为什么你需要这个......这可能对决定如何做到这一点很重要......这是我的尝试。

为什么不设计 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文