【问题标题】:How to bind multiple ComboBoxes to one Collection? [duplicate]如何将多个组合框绑定到一个集合? [复制]
【发布时间】:2018-11-28 15:37:13
【问题描述】:

我需要一个组合框的集合,其中包含可能的选择的公共集合。

代码隐藏摘录:

namespace ComboBoxesInCollection
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            D = new DataContainer();
            this.DataContext = D;
        }

        private DataContainer D;
    }

    public class DataContainer
    {
        public ObservableCollection<Item> ComboboxItems
        {
            get { return comboboxItems; }
        }

        public ObservableCollection<Selection> ComboboxSelections
        {
            get { return comboboxSelections; }
        }

        public DataContainer()
        {
            comboboxItems = new ObservableCollection<Item>
                    {
                        new Item(100, "Entry #1"),
                        new Item(101, "Entry #2"),
                        new Item(102, "Entry #3")
                    };

            comboboxSelections = new ObservableCollection<Selection>()
                    {
                        new Selection(1),
                        new Selection(2),
                        new Selection(3)
                    };
        }

        private ObservableCollection<Item> comboboxItems;
        private ObservableCollection<Selection> comboboxSelections;
    }
}

XAML 摘录:

    <Window.Resources>

        <DataTemplate x:Key="CSTemplate">
            <Border BorderBrush="Black" BorderThickness="1,1,0,0" Margin="0,0,0,4" Padding="4">
                <StackPanel>
                    <Label Content="{Binding Id}"/>
                    <ComboBox 
                     ItemsSource="{Binding ComboboxItems}" //<= does not work
                     DisplayMemberPath="Name" 
                     SelectedValue="{Binding SelectedId}" 
                     SelectedValuePath="Id" 
                    />
                </StackPanel>
            </Border>
        </DataTemplate>

    ...            

        <ItemsControl ItemsSource="{Binding ComboboxSelections}" ItemTemplate="{StaticResource CSTemplate}"/>

ItemsControl 显示项目,但 Combobox 为空。

我知道我尝试访问当前不存在的 Selection 内的属性/集合。

如何正确指定 DataBinding 以便我可以看到项目?

【问题讨论】:

  • ItemsControl 的 ItemTemplate 中元素的 DataContext 是其 ItemsSource 集合中的关联元素,即一个 Selection 对象。为了绑定到“父”视图模型,编写像ItemsSource="{Binding DataContext.ComboboxItems, RelativeSource={RelativeSource AncestorType=ItemsControl}}"这样的绑定
  • 我假设您希望看到一个组合框显示值 1,2 和 3 ?还是 3 个组合框,每个组合框连续显示 1,2 和 3?
  • 如果这是静态数据,那么您可以将其设为资源。您可以将 observablecollection 放入具有默认设计器数据的资源中,并在运行时从数据库中填充它。 social.technet.microsoft.com/wiki/contents/articles/…
  • @auburg 三个组合框显示 1,2 和 3
  • @Clemens 您的建议有效,这正是我想要的,谢谢!您可能希望将其添加为实际答案

标签: wpf data-binding


【解决方案1】:

使用元素绑定来访问窗口的DataContext 属性。

首先命名窗口x:Name="Window1" 和while 绑定,使用元素绑定。

 ItemsSource="{Binding ElementName=Window1, Path=DataContext.ComboboxItems}" 

整个窗口的XAML

<Window x:Class="WpfApp6.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:WpfApp6"
    mc:Ignorable="d"
    Title="MainWindow" Height="450" Width="800" x:Name="Window1">
<Window.Resources>

    <DataTemplate x:Key="CSTemplate">
        <Border BorderBrush="Black" BorderThickness="1,1,0,0" Margin="0,0,0,4" Padding="4">
            <StackPanel>
                <Label Content="{Binding Id}"/>
                <ComboBox 
                    ItemsSource="{Binding ElementName=Window1, Path=DataContext.ComboboxItems}" 
                DisplayMemberPath="Name" 
                SelectedValue="{Binding SelectedId}" 
                SelectedValuePath="Id" 
                />
            </StackPanel>
        </Border>
    </DataTemplate>
    </Window.Resources>
<Grid>
    <ItemsControl ItemsSource="{Binding ComboboxSelections}" ItemTemplate="{StaticResource CSTemplate}" />
</Grid>

【讨论】:

  • @DennisChristian 查看解决方案。
  • 我做到了,它有效,谢谢。而不是使用对窗口元素的命名引用,我更喜欢使用某种类型的绑定导航,就像克莱门斯建议的那样
猜你喜欢
  • 2022-01-11
  • 1970-01-01
  • 2016-08-23
  • 1970-01-01
  • 1970-01-01
  • 2015-10-19
  • 1970-01-01
  • 1970-01-01
  • 2016-04-23
相关资源
最近更新 更多