如何将多个ComboBox绑定到一个Collection?

时间:2018-11-28 15:37:14

标签: wpf data-binding

我需要一个ComboBoxes集合,其中包含可能选择的通用集合。

代码隐藏摘录:

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显示项目,但组合框为空。

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

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

1 个答案:

答案 0 :(得分:0)

使用元素绑定访问窗口的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>