WPF ComboBox绑定到ObservableCollection [英] WPF ComboBox Binding to ObservableCollection

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

问题描述

我是WPF的新手,我有一个问题. 我有组织"模块:

I'm new to WPF And I have a question. I have the Organization module:

class Organization : ObservableObject
{
    public string OrganizationName { get; set; }
}

我拥有组织的ViewModel:

I have the ViewModel of the Organization:

class OrganizationViewModel : ObservableObject
{
    int _count = 0;

    public OrganizationViewModel()
    {
        Organization = new Organization {OrganizationName = "New Organization"};
    }

    public Organization Organization { get; set; }

    public string OrganizationName
    {
        get { return Organization.OrganizationName; }
        set
        {
            if(Organization.OrganizationName != value)
            {
                Organization.OrganizationName = value;
                RaisePropertyChanged("OrganizationName");
            }
        }
    }

我拥有所有组织的ViewModel:

And I have the ViewModel of all the Organizations:

class AllOrganizationsViewModel
    {
        private ObservableCollection<OrganizationViewModel> m_organizations = new ObservableCollection<OrganizationViewModel>();


    public ObservableCollection<OrganizationViewModel> Organizations
    {
        get { return m_organizations; }
        set { m_organizations = value; }
    }

    public AllOrganizationsViewModel()
    {
        for(int i = 0; i < 3; ++i)
        {
            m_organizations.Add(new OrganizationViewModel());
        }
    }

    void AddOrganizationNameExecute()
    {
        m_organizations.Add(new OrganizationViewModel());
    }

    bool CanAddOrganizationNameExecute()
    {
        return true;
    }

    public ICommand AddOrganization{get{return new RelayCommand(AddOrganizationNameExecute, CanAddOrganizationNameExecute);}}

}

这是MainWindow.xaml:

And this is the MainWindow.xaml:

<Window x:Class="DataIntegrityChecker.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:DataIntegrityChecker.ViewModels" Title="MainWindow" Height="350" Width="525">
    <Window.DataContext>
        <local:AllOrganizationsViewModel />
    </Window.DataContext>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="285*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="156" />
            <ColumnDefinition Width="347*" />
        </Grid.ColumnDefinitions>
        <Label Content="Organization: " Margin="0,0,44,0" />
        <Button Grid.Row="1" Name="UpdateOrganizations" Content="Update Organization Name" Command="{Binding AddOrganization}" Margin="0,0,0,262" HorizontalAlignment="Left" Width="156" />
        <ComboBox Grid.Column="1" Grid.Row="1" Height="23" HorizontalAlignment="Left" Name="comboBox1" VerticalAlignment="Top" Width="155"  ItemsSource="{Binding Organizations}" />
    </Grid>
</Window>

现在,我在ComboBox项中得到的是类OrganizationViewModel的名称,但我需要的是OrganizationName字符串.在我看来,我缺少与OrganizationName的绑定,但是我不知道该在哪里添加(如果这就是问题所在).我假设我可以使用我需要的组织名称来收集字符串.但是将来,我将在组织类"中需要更多属性,这就是我需要它工作的方式.

Now, what I get in the ComboBox items is the name of the class OrganizationViewModel but what I need is the OrganizationName string. It seems to me that I have some binding to OrganizationName that's missing but I can't figure out where to add if (if that's the issue). I assume I can make a collection of strings with the names of the organizations I need. But in the future I will need more properties in the Organization Class so that's the way I need it to work.

我将不胜感激

推荐答案

您需要添加DisplayMemberPath:

<ComboBox Grid.Column="1" Grid.Row="1" Height="23" HorizontalAlignment="Left" Name="comboBox1" VerticalAlignment="Top" Width="155"  ItemsSource="{Binding Organizations}"
          DisplayMemberPath="OrganizationName"/>

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

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