本文介绍了Wpf列表框文本但发送值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我被困住了。我有一个带有列表框的WPF页面,该列表框具有来自codebehind(.cs)文件的绑定值。显示的文本是Active,Pending,Discharged等。当我选择Active时,我需要得到字符串A。我该怎么做? 在XAML中 DisplayMemberPath =ContentNameItemsSource ={Binding}SelectedValuePath =ContentCode> 在CS public PatientSearch() { InitializeComponent(); var data = new [] { new ListBoxData {ContentCode = A,ContentName = 有效}, new ListBoxData {ContentCode = P,ContentName = 待定}, new ListBoxData {ContentCode = D,ContentName = 放电}, new ListBoxData {ContentCode = I,ContentName = 中断}, new ListBoxData {ContentCode = X,ContentName = 已存档} }; // 使用ListBox绑定数组列表 LbPatStat.ItemsSource = data; } public class ListBoxData { public string ContentCode { get ; set ; } public string ContentName { get ; set ; } } 我的尝试: 我试图使用IF / Else和Switch语句,但每个语句都通过Active而不是A ... 解决方案 只需在ListBox SelectionChanged事件中执行以下操作, private void LbPatStat_SelectionChanged( object sender,SelectionChangedEventArgs e) { ListBoxData SelectedListBoxData =(ListBoxData)LbPatStat.SelectedItem; MessageBox.Show( SelectedListBoxData: + SelectedListBoxData.ContentCode); } I am stuck. I have a WPF page with a listbox that has binding values from codebehind(.cs) file. The text displayed is Active, Pending, Discharged, etc. When I select Active, I need to get string "A". How do I do this?In the XAMLDisplayMemberPath="ContentName" ItemsSource="{Binding}" SelectedValuePath="ContentCode">In the CSpublic PatientSearch(){ InitializeComponent(); var data = new[] { new ListBoxData {ContentCode="A", ContentName="Active"} , new ListBoxData {ContentCode="P", ContentName="Pending"} , new ListBoxData {ContentCode="D", ContentName="Discharge"} , new ListBoxData {ContentCode="I", ContentName="Interrupted"}, new ListBoxData {ContentCode = "X", ContentName = "Archived"} }; // Bind Array List with the ListBox LbPatStat.ItemsSource = data;}public class ListBoxData{ public string ContentCode { get; set; } public string ContentName { get; set; }}What I have tried:I have tried to use IF/Else ans Switch statements, but each of them pass Active and not A... 解决方案 Hi, just do the following in ListBox SelectionChanged event,private void LbPatStat_SelectionChanged(object sender, SelectionChangedEventArgs e){ ListBoxData SelectedListBoxData = (ListBoxData)LbPatStat.SelectedItem; MessageBox.Show("SelectedListBoxData : " + SelectedListBoxData.ContentCode);} 这篇关于Wpf列表框文本但发送值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-27 14:00