I'm adding dynamically an itemsource to the datagrid:
datagrid.ItemsSource = _table.DefaultView;
foreach (DataColumn column in _table.Columns)
{
if (column.DataType == typeof(bool))
{
var dgrcl = new DataGridCheckBoxColumn
{
IsThreeState = false,
Header = column.Caption,
Binding = new Binding(column.ColumnName),
Width = new DataGridLength(15, DataGridLengthUnitType.Star)
};
datagrid.Columns.Add(dgrcl);
}
else
{
var dgrcl = new DataGridTextColumn();
dgrcl.Binding = new Binding(column.ColumnName);
dgrcl.Header = column.Caption;
datagrid.Columns.Add(dgrcl);
}
}
But if I add a new row to Datagrid (with checkboxcolumn) - the checkbox is threestate. I tried to add next code in xaml:
<Style TargetType="CheckBox" x:Key="dgrChkBoxStyle">
<Setter Property="IsThreeState" Value="False"></Setter>
<Setter Property="IsChecked" Value="True"></Setter>
</Style>
<DataGrid Name="datagrid" Grid.ColumnSpan="2" HorizontalAlignment="Stretch" Margin="10" VerticalAlignment="Stretch" AlternatingRowBackground="Honeydew" AlternationCount="2" AutoGenerateColumns="False">
<DataGridCheckBoxColumn>
<DataGridCheckBoxColumn.ElementStyle>
<Style TargetType="CheckBox" BasedOn="{StaticResource dgrChkBoxStyle}"></Style>
</DataGridCheckBoxColumn.ElementStyle>
</DataGridCheckBoxColumn>
</DataGrid>
But there is exception that "Items collection must be empty before using ItemsSource".
I am new in WPF,please any advise, how to make checkboxes with only two states?