绑定到ObservableCollection的WPF组合框

tp5buhyn  于 2022-11-18  发布在  其他
关注(0)|答案(3)|浏览(166)

我是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);}}

}

这是主窗口.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的绑定,但如果(如果这是个问题的话)我假设我可以用我需要的组织的名称创建一个字符串集合。但是在将来,我将需要更多的属性在组织类中,所以这是我需要它的工作方式。
我将感激任何帮助

vcirk6k6

vcirk6k61#

您需要添加DisplayMemberPath

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

swvgeqrz2#

另一种方法是通过定义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>
monwx1rj

monwx1rj3#

这是我的建议,以解决您的问题:
代码隐藏:

private ObservableCollection<customObj> customObjs = new ObservableCollection<customOb>();
 public ObservableCollection<customObj> CustomObjs
    {
        get => customObj;
        set
        {
            customObj = value;
        }
    }

public class customObj
{
    private uint number;
    public uint Number
    {
        get { return number; }
        set
        {
            if (number != value)
            {
                number = value;
            }
        }
    }
}

在Xaml文件中:

<Style x:Key="ComboBoxCustomObjStyle" TargetType="{x:Type ComboBox}">
    <Setter Property="ItemsSource" Value="{Binding Path=CustomObjs}"/>
    <Setter Property="DisplayMemberPath" Value="Number"/>
    <Setter Property="SelectedValuePath" Value="Number"/>
    <Setter Property="SelectedValue" Value="0"/>
</Style>  
<ComboBox x:Name="CbSelectCustomObj"               
                  Style="{StaticResource ComboBoxCustomObjStyle}"/>

请记住,在显示组合框之前,请确保已填充可观察集合!

相关问题