如何将WPF ListBox与ComboBox绑定 [英] How to bind WPF ListBox with ComboBox

查看:572
本文介绍了如何将WPF ListBox与ComboBox绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带ComboBox的ListBox。 ListBox的项目源已设置为  ObservableCollection。

I have a ListBox with ComboBox. The item source of the ListBox has been set to an ObservableCollection.

如何将ComboBox ItemSource设置为View Model中的另一个ObservableCollection。

How to set the ComboBox ItemSource to another ObservableCollection which is inside the View Model.

这是我的代码:

    public class MyViewModel
    {
        public ObservableCollection<Employee> Employees { get; set; }

        public ObservableCollection<string> Designations { get; set; }

        public MyViewModel()
        {
            Employees = ReadEmolpyeesFromDB();
            Designations = ReadDesignationsFromDB();
        }

        private ObservableCollection<Employee> ReadEmolpyeesFromDB()
        {
            // read DB and return ObservableCollection<Employee>
        }

        private ObservableCollection<string> ReadDesignationsFromDB()
        {
            // read DB and return ObservableCollection<string>
        }

    }

    public class Employee
    {
        public string Name { get; set; }
        public int Age { get; set; }
        public string Designation { get; set; }
    }


        <ListBox x:Name="Lv_Employees" ItemsSource="{Binding Employees}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid >
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="200" />
                            <ColumnDefinition Width="40" />
                            <ColumnDefinition Width="100" />
                        </Grid.ColumnDefinitions>
                        <TextBox Text="{Binding Name}" />
                        <TextBox Text="{Binding Age}" Grid.Column="1"/>
                        <ComboBox SelectedValue="{Binding Designation}" Grid.Column="2"
                                  ItemsSource="{Binding Designations}"/>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

推荐答案

抱歉不在VB工作站附近,因此无法显示工作代码,但概念是:

Sorry not near a VB workstation so can't show you working code but the concept is :

1: 你有一个在某个级别定义的DataContext(对于这个让我们说在窗口级别)

1:  You have a DataContext defined at a certain level (for this lets say at the window level)

2:  ItemsSource(Employees)在ViewModel(datacontext)中定义。 我相信这对你有用。

2:  The ItemsSource (Employees) is defined in your ViewModel (datacontext).  This I am sure works for you.

3:  ComboBox的ItemsSource应该看起来像 -

3:  The ItemsSource for the ComboBox should look like -

"{Binding DataContext.Designations, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Window}}}"

这样做是为了定义你的datacontext(在本例中为Window),然后使用Window的DataContext和Designations属性。

What this is doing is going to the level that defines your datacontext (in this case Window) and then uses the DataContext of the Window and the Designations property.


这篇关于如何将WPF ListBox与ComboBox绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆