I am trying to fill a ListBox with items consisting of a grid which holds a checkbox and a textblock. I originally had only a checkbox but I need the text to wrap so I changed it to a grid with a checkbox and a textblock, with the text block doing the text wrapping.
When it was just the the checkbox, the binding worked, but now it does not. Now the correct number of items show up in the listbox but it is just an unchecked checkbox (even if I set it to checked in the code behind, see below) and a blank textblock.
XAML:
<ListBox
x:Name="lstCalibration"
Margin="304,95,78,24"
Background="#7FFFFFFF"
Width="211"
HorizontalAlignment="Center"
VerticalContentAlignment="Stretch"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
HorizontalContentAlignment="Stretch">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Width="auto">
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<CheckBox
Content=""
Tag="{Binding Tag}"
IsChecked="{Binding IsChecked}"
Checked="CheckBoxCal_Checked"
Unchecked="CheckBoxCal_Unchecked"
Grid.Row="0" Grid.Column="0"
/>
<TextBlock
Tag="{Binding Tag}"
Text="{Binding Text}"
TextWrapping="Wrap"
Grid.Row="0"
Grid.Column="1"
/>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Code behind:
foreach (Controller item in calList)
{
//fill listbox with cals and correct checked states
Grid grid = new Grid();
CheckBox cb = new CheckBox();
TextBlock tb = new TextBlock();
tb.Text = item.Description;
cb.Tag = tb.Tag = i;
cb.IsChecked = calChecked[i];
if (cb.IsChecked == true)
noneChecked = false;
i++;
Grid.SetRow(cb, 0);
Grid.SetColumn(cb, 0);
grid.Children.Add(cb);
Grid.SetRow(tb, 0);
Grid.SetColumn(tb, 1);
grid.Children.Add(tb);
lstCalibration.Items.Add(grid);
}