gpt4 book ai didi

c# - WPF ComboBox 绑定(bind)到 ObservableCollection

转载 作者:太空狗 更新时间:2023-10-29 18:06:44 39 4
gpt4 key购买 nike

我是 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 字符串。在我看来,我对缺少的 OrganizationName 有一些绑定(bind),但我不知道在哪里添加 if(如果这是问题所在)。我假设我可以用我需要的组织的名称制作一个字符串集合。但将来我将需要 Organization Class 中的更多属性,这就是我需要它工作的方式。

我将不胜感激

最佳答案

您需要添加DisplayMemberPath:

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

关于c# - WPF ComboBox 绑定(bind)到 ObservableCollection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16197928/

39 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com