WPF组合框绑定到的ObservableCollection(WPF ComboBox Bindin

2019-09-01 02:24发布

我是新来WPF和我有一个问题。 我有本组织模块:

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

我有本组织的视图模型:

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");
            }
        }
    }

我有所有的组织视图模型:

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>

现在,我所得到的组合框项目是类OrganizationViewModel的名字,但我需要的是单位名称的字符串。 在我看来,我的一些结合单位名称是不可少的,但我不能计算出,如果在哪里添加(如果是这样的问题)。 我以为我可以使字符串的集合与我所需要的组织的名称。 但在将来,我会所以这是我需要它的工作方式需要在组织类更多的属性。

我感谢所有帮助

Answer 1:

您需要添加DisplayMemberPath

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


Answer 2:

另一种方式可以通过定义的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>


文章来源: WPF ComboBox Binding to ObservableCollection