Answered by:
How to binding the Description and ID of the combobox to a class

Question
-
I want to bind the description and id to a class on WPFSaturday, August 13, 2016 5:00 PM
Answers
-
The ComboBox has no Description property. It also has no ID property so your question (like the other) is confusing and hard to help.
Lloyd Sheen
- Proposed as answer by DotNet Wang Sunday, August 21, 2016 3:11 AM
- Marked as answer by DotNet Wang Wednesday, August 24, 2016 1:52 AM
Saturday, August 13, 2016 5:03 PM -
Bind the SelectedItem property to a source property and access the id and description of the object returned by this source property. Please refer to the following sample code which should give you the idea:
<ComboBox ItemsSource="{Binding Items}" DisplayMemberPath="Description" SelectedItem="{Binding SelectedItem}" /> <TextBlock Text="{Binding SelectedItem.Id}" /> <TextBlock Text="{Binding SelectedItem.Description}" />
public class MyItem { public string Id { get; set; } public string Description { get; set; } }
public partial class MainWindow : Window, INotifyPropertyChanged { public MainWindow() { InitializeComponent(); DataContext = this; Items = new List<MyItem>() { new MyItem() { Id = "1", Description = "desc1" }, new MyItem() { Id = "2", Description = "desc1" } }; } public IEnumerable<MyItem> Items { get; set; } private MyItem _selectedItem; public MyItem SelectedItem { get { return _selectedItem; } set { _selectedItem = value; NotifyPropertyChanged(); } } public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged([CallerMemberName] String propertyName = "") { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } }
Hope that helps.
Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.
- Proposed as answer by DotNet Wang Sunday, August 21, 2016 3:11 AM
- Marked as answer by DotNet Wang Wednesday, August 24, 2016 1:52 AM
Saturday, August 13, 2016 5:10 PM
All replies
-
The ComboBox has no Description property. It also has no ID property so your question (like the other) is confusing and hard to help.
Lloyd Sheen
- Proposed as answer by DotNet Wang Sunday, August 21, 2016 3:11 AM
- Marked as answer by DotNet Wang Wednesday, August 24, 2016 1:52 AM
Saturday, August 13, 2016 5:03 PM -
Bind the SelectedItem property to a source property and access the id and description of the object returned by this source property. Please refer to the following sample code which should give you the idea:
<ComboBox ItemsSource="{Binding Items}" DisplayMemberPath="Description" SelectedItem="{Binding SelectedItem}" /> <TextBlock Text="{Binding SelectedItem.Id}" /> <TextBlock Text="{Binding SelectedItem.Description}" />
public class MyItem { public string Id { get; set; } public string Description { get; set; } }
public partial class MainWindow : Window, INotifyPropertyChanged { public MainWindow() { InitializeComponent(); DataContext = this; Items = new List<MyItem>() { new MyItem() { Id = "1", Description = "desc1" }, new MyItem() { Id = "2", Description = "desc1" } }; } public IEnumerable<MyItem> Items { get; set; } private MyItem _selectedItem; public MyItem SelectedItem { get { return _selectedItem; } set { _selectedItem = value; NotifyPropertyChanged(); } } public event PropertyChangedEventHandler PropertyChanged; private void NotifyPropertyChanged([CallerMemberName] String propertyName = "") { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } }
Hope that helps.
Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't ask several questions in the same thread.
- Proposed as answer by DotNet Wang Sunday, August 21, 2016 3:11 AM
- Marked as answer by DotNet Wang Wednesday, August 24, 2016 1:52 AM
Saturday, August 13, 2016 5:10 PM