[英]ComboBox Select Item based on Object
so im doing like a little Customer Management programm, just for learning purpose.所以我就像一个小的客户管理程序,只是为了学习目的。
I have a class "Customer" which contains: Name, LastName, Age, Gender and some more.我有一个“客户”类,其中包含:姓名、姓氏、年龄、性别等等。 Im Loading my Data from a Json file and im populating an Observable Collection with it.
我从一个 Json 文件加载我的数据并用它填充一个 Observable 集合。 Now as my Screenshot shows below, i want a combobox which displays the Gender of my Selected Customer and its kinda working.
现在,如下面的屏幕截图所示,我想要一个组合框,显示所选客户的性别及其工作原理。 the Problem is that the ComboBox has as many options to chose from as i got Customers in my Observable Collection.
问题是 ComboBox 有很多选项可供选择,因为我在 Observable 集合中有客户。 So i want to edit the Gender from my selected Customer with my Combobox, therefore it should only have 2 Options to chose from: Male, Female.
所以我想用我的组合框编辑我选择的客户的性别,因此它应该只有 2 个选项可供选择:男性、女性。
XAML: XAML:
Binding Path Customers is the name of my Observable Collection.绑定路径客户是我的 Observable 集合的名称。
<ComboBox x:Name="ComboBoxGender"
ItemsSource="{Binding Path=Customers}"
DisplayMemberPath="Gender"
SelectedValuePath="Gender"
SelectedValue="{Binding Path=SelectedCustomer.Gender}"
Grid.Row="3" HorizontalAlignment="Right"
VerticalAlignment="Center"
Height="20" Width="130" Margin="0,0,5,0">
</ComboBox>
Thanks for your help!谢谢你的帮助! (sorry for my bad grammar..)
(抱歉我的语法不好..)
So i got it running fine now.所以我现在运行良好。 I Changed my Customer Class:
我改变了我的客户类别:
int Age {get; set;}
....
public Gender _Gender { get; set; }
public enum Gender
{
Male, Female
}
And i changed my XAML:我改变了我的 XAML:
<ComboBox x:Name="ComboBoxGender"
ItemsSource="{Binding Genderlist}"
DisplayMemberPath="_Gender"
SelectedValuePath="_Gender"
SelectedValue="{Binding SelectedCustomer._Gender}"
Grid.Row="3" HorizontalAlignment="Right"
VerticalAlignment="Center"
Height="20" Width="130" Margin="0,0,5,0">
</ComboBox>
My Itemssource is:我的物品来源是:
private ObservableCollection<Customer> _genders = new ObservableCollection<Customer>();
public MainModel()
{
_genders.Add(new Customer { _Gender = Customer.Gender.Herr });
_genders.Add(new Customer { _Gender = Customer.Gender.Frau });
}
Now its working fine, just for someone who has the same problem in the future :)现在它工作正常,只适用于将来遇到同样问题的人:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.