C#/WPF中ComboBox的ItemData [英] ItemData for ComboBox in C# /WPF

查看:74
本文介绍了C#/WPF中ComboBox的ItemData的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对C#/WPF很陌生.我正在尝试将组合框绑定到由XAML中具有成员ID和名称的类封装的项目列表.我实际上是在组合框中显示名称.我还需要将相应的ID与每个名称相关联,以便 从组合中选择名称后,我可以获得相关的ID.在WPF中有什么方法可以做到这一点吗? IE.有什么办法也可以绑定id吗?以前有带组合框的ItemData属性.

I am quite new to C#/WPF. I am trying to bind a combo box to a list of items encapsulated by a class with members id and name in XAML. I am actually displaying the name in the combo box. I also need to associate the corresponding id to each name so that on selecting the name from the combo , I can get the associated id. Is there any method to do this in WPF? ie. is there any way to bind the id also ? Earlier there was the ItemData property with combo boxes.

推荐答案

< g class =" gr_ gr_12 gr-alert gr_spell gr_run_anim ContextualSpelling" data-gr-id ="12". id ="12"> Lets/g.说您的项目在名为Member的类中,该类具有两个属性(非常重要的是它们是属性) 编号和名称.从哪里您的数据< g class =" gr_ gr_225 gr-alert gr_gramm gr_run_anim仅语法-insertWithoutSep data-gr-id ="225". id ="225"/g存在.创建成员的集合(列表即可).  这将成为您的ItemsSource.  然后,您将为包含ComboBox的窗口创建该窗口的DataContext,该属性是该集合.

<g class="gr_ gr_12 gr-alert gr_spell gr_run_anim ContextualSpelling" data-gr-id="12" id="12">Lets</g> say your items are in a class called Member with two properties (very important that they are properties) Id and Name. From where ever your data <g class="gr_ gr_225 gr-alert gr_gramm gr_run_anim Grammar only-ins replaceWithoutSep" data-gr-id="225" id="225">exists</g> create a collection (a List would do) of Member(s).  This will become your ItemsSource.  Then you would create your DataContext for the Window containing the ComboBox with a property which is that collection.

< g class =" gr_ gr_374 gr-alert gr_gramm gr_run_anim标点多重替换"data-gr-id ="374" id ="374"> next//g.您需要为ComboBox创建XAML.

<g class="gr_ gr_374 gr-alert gr_gramm gr_run_anim Punctuation multiReplace" data-gr-id="374" id="374">Next</g> you need to create your XAML for the ComboBox.

<ComboBox

ItemsSource =" {Binding yourCollection}"

ItemsSource="{Binding yourCollection}"

DisplayMemberPath =" {Binding Name}"

DisplayMemberPath="{Binding Name}"

SelectedItem =" {Binding theSelectedItem}">

SelectedItem="{Binding theSelectedItem}">

</ComboBox>

</ComboBox>

还在您的DataContext对象中创建一个名为theSelectedItem的属性作为成员.  这将允许您获取用户在ComboBox中选择的Member项(注意,未使用任何事件).  

Also in your DataContext object create a property called theSelectedItem as a Member.  This will allow you to get the Member item that was selected in the ComboBox by the user (notice no events used).  

这应该让您入门.


这篇关于C#/WPF中ComboBox的ItemData的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆