Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do you bind a collection of items to a list box of checkboxes?
    primarykey
    data
    text
    <p>Sorry for the vague description, I can't think of a better way to put it.</p> <p>Let's say that my ViewModel has a property as follows:</p> <pre><code>public List&lt;MyClass&gt; SubSystems { get; set; } </code></pre> <p>and the SubSystems class:</p> <pre><code>public class SubSystem { public string Name { get; set; } public bool IsSelected { get; set; } } </code></pre> <p>In the view, I'd like to bind the <code>SubSystems</code> property to, what I think would be, a list of checkboxes where the IsChecked and Name properties of the CheckBox is bound to the their respective properties, IsChecked for IsSelected and Content for Name.</p> <p>I know I can make a ListBox in the XAML, but I'm not sure how I'd go about doing this using binding and a collection..</p> <p>Thanks for the help!</p> <p>Edit -</p> <p>Here's the XAML:</p> <pre><code>&lt;GroupBox Header="Sub-Systems" Grid.Column="0" Grid.Row="0" Margin="5"&gt; &lt;Grid&gt; &lt;Grid.Resources&gt; &lt;DataTemplate x:Key="checkBox"&gt; &lt;StackPanel Orientation="Horizontal"&gt; &lt;CheckBox IsChecked="{Binding IsSelected}" /&gt; &lt;TextBlock Text="{Binding Name}" /&gt; &lt;/StackPanel&gt; &lt;/DataTemplate&gt; &lt;/Grid.Resources&gt; &lt;ListBox ItemTemplate="{StaticResource checkBox}" ItemsSource="{Binding SubSystems}" /&gt; &lt;/Grid&gt; &lt;/GroupBox&gt; </code></pre> <p>Edit #2 -</p> <p>Just to clarify, all of the examples populate the box, but none of the examples are breaking on the breakpoints in the setters.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload