I'm having a Collection of type Employee Model Class. It has two properties empName and isChecked.
Expectation : I need to update the property isChecked from the Checkbox, while on Clicking the Apply Button. Otherwise it don't need to update the Property.
void Main()
{
Dictionary<int, List<Employee>> empList = new Dictionary<int, List<Employee>>()
{
{1, new List<Employee>() { new Employee() {empName = "Raj"}, new Employee() {empName = "Kumar"}}},
{2, new List<Employee>() { new Employee() {empName = "Bala"}}},
{3, new List<Employee>() { new Employee() {empName = "Manigandan"}}},
{4, new List<Employee>() { new Employee() {empName = "Prayag"}, new Employee() {empName = "Pavithran"}}},
{5, new List<Employee>() { new Employee() {empName = "Selva"}}},
};
empList.Dump();
}
public class Employee
{
public string empName { get; set; }
public bool isChecked { get; set; }
}
I Binded this Collection into a WPF ListBox using MVVM approach. The empName is Binded with TextBlock and isChecked is Binded with Checkbox.
<ListBox ItemsSource="{Binding empList.Values, IsAsync=True, UpdateSourceTrigger=Explicit}">
<cust:BListBox.ItemTemplate>
<DataTemplate>
<CheckBox IsChecked="{Binding isChecked, UpdateSourceTrigger=Explicit}">
<CheckBox.Content>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding empName, IsAsync=True}" Visibility="Visible" />
</StackPanel>
</CheckBox.Content>
</CheckBox>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Button Content="Apply" Command="{Binding ApplyChangesCommand}"/>
The Command for the Apply Button is
public ICommand ApplyChangesCommand
{
get
{
return new DelegatingCommand((object param) =>
{
/// Logical Code.
});
}
}
Note: Kindly use MVVM approach.
>` then the `SelectedItem's` type should be `List` and in the `ApplyChanges` method you will use the foreach to iterate through the `List SelectedItem`. `item.Model.IsChecked = item.IsChecked`.
– user2250152 Apr 11 '16 at 12:05