Hi
I have made checked List Box in WPF but the binding is not working correctly.I checked the List i created it contains the data in correct Format but when i bind it with the List box it does not work
only Last entry appears in list box number of item times that are in List
Checked Box List XML
<ListBox x:Name="list" Margin="18,100,535,74">
<ListBox.ItemTemplate>
<HierarchicalDataTemplate>
<my:RibbonCheckBox Label="{Binding Name}" IsChecked="{Binding IsChecked}" />
</HierarchicalDataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Class For data holding
public class CheckedListItem
{
public int Id { get; set; }
public string Name { get; set; }
public bool IsChecked { get; set; }
}
Button to Populate List Box
private void button1_Click(object sender, RoutedEventArgs e)
{
List<CheckedListItem> AvailablePresentationObjects = new List<CheckedListItem>();
CheckedListItem item = new CheckedListItem();
for (int i = 0; i < 10; i++)
{
item.Id = i;
item.Name = i.ToString();
item.IsChecked = false;
AvailablePresentationObjects.Add(item);
}
list.ItemsSource = AvailablePresentationObjects;
}
You instantiate your
CheckedListItem item = new CheckedListItem();outside of the for loop, and only initialize one instance ofCheckedListItemwhich you edit 10 times. Only the last edit remains.You also added this very same instance, 10 times to the
AvailablePresentationObjects.Try the following: