Answer the question
In order to leave comments, you need to log in
How to make a dynamic CheckBox list in WPF?
Good evening.
There is a list List<KeyValuePair<di, string>> _regionsList;
in it id and the name of the region is stored.
How to display this data in a ListBox as a CheckBox with Content the name of the region?
And how to make it so that you can put N checkmarks.
Answer the question
In order to leave comments, you need to log in
In the simplest case, it should look something like this:
XAML:
<ListBox x:Name="lst">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Value}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
lst.ItemsSource = new List<KeyValuePair<int, string>>()
{
new KeyValuePair<int, string>(1, "1"),
new KeyValuePair<int, string>(2, "2"),
};
class MyClass
{
public int id { get; set; }
public string Name { get; set; }
public bool IsChecked { get; set; }
}
<ListBox x:Name="lst">
<ListBox.ItemTemplate>
<DataTemplate>
<CheckBox Content="{Binding Name}" IsChecked="{Binding IsChecked}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
lst.ItemsSource = new List<MyClass>()
{
new MyClass(){id=1, Name="1"},
new MyClass(){id=2, Name="2"},
};
foreach(var tObj in (lst.ItemsSource as List<MyClass>).Where(myObj => myObj.IsChecked))
MessageBox.Show(tObj.Name);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question