WPF ComboBox绑定到ObservableCollection

时间:2013-04-24 16:59:36

标签: c# wpf mvvm combobox observablecollection

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

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

我有本组织的ViewModel:

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:

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:

<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字符串。在我看来,我有一些组织名称的绑定,但我无法弄清楚在哪里添加if(如果这是问题)。我假设我可以使用我需要的组织的名称来创建字符串集合。但是在将来,我将需要组织类中的更多属性,这就是我需要它工作的方式。

我将不胜感激任何帮助

2 个答案:

答案 0 :(得分:13)

您需要添加DisplayMemberPath

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

答案 1 :(得分:6)

另一种方法可以是定义ItemTemplate。它在显示上提供了更大的灵活性

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