Binding Ischecked of a checkbox in wpf listbox on selectionchanged of the combobox
I have a combobox of customer and that customer can be in more than 1 categories so i used a listbox which contains check-boxes of all the categories...
On the selection change of the customer, the categories in which a particular customer is, should be checked and all the other categories should remain unchecked..
here is my .dbml file
Here is my xaml code of listbox...
<ListBox Height="113.88" Margin="399.342,125.543,424.66,0" Name="lst_category" VerticalAlignment="Top" SelectedValuePath="CategoryID">
<ListBox.ItemTemplate>
开发者_Python百科 <HierarchicalDataTemplate>
<CheckBox Content="{Binding CategoryName}"/>
</HierarchicalDataTemplate></ListBox.ItemTemplate>
</ListBox>
I think i should use relative source in the binding in Ischecked property of checkbox... But i dont know how to use it please help me out... If there is some other solution to this than do let me know.. Thanks in advance...
Solution 1: Create a class CategoryViewModel like this:
class CategoryViewModel : INotifyPropertyChanged
{
public Category Category {get ... set ...}
public bool IsChecked {get ... set ...} //true if Category belongs to currently selected contact
}
Bind your UI to a ViewModel class that contains a list of CategoryViewModel that gets computed whenever you change the Selected Contact.
Basically:
class ViewModel : INotifyPropertyChanged
{
public Contact SelectedContact { get .... set ....}
//list of all possible categories (the ones belonging to SelectedContact will have IsChecked true
public ObservableCollection<CategoryViewModel> Categories
{
get .... set ....
}
}
Bind your listbox above to ViewModel.Categories property.
SelectedContact should be bound to the currently selected contact. When it changes, in the setter, you re-create Categories list.
Solution 2: Use some converters (wouldn't recommend it thought, because it's not MVVM)
精彩评论