1
votes

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?

2

2 Answers

0
votes

you should write the column definition like so :

  <DataGrid Name="datagrid" Grid.ColumnSpan="2" HorizontalAlignment="Stretch" Margin="10" VerticalAlignment="Stretch" AlternatingRowBackground="Honeydew" AlternationCount="2" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridCheckBoxColumn>
                <DataGridCheckBoxColumn.ElementStyle>
                    <Style TargetType="CheckBox" BasedOn="{StaticResource dgrChkBoxStyle}"></Style>
                </DataGridCheckBoxColumn.ElementStyle>
            </DataGridCheckBoxColumn>
        </DataGrid.Columns>
    </DataGrid>
0
votes

You could access the DataGridCheckBoxColumn directly. How about?

<Style x:Key="DgCheckBoxColumnStyle" x:TargetType="{x:Type DataGridCheckBoxColumn}">
    <Setter Property="IsThreeState" Value="False" />
    <Setter Property="Binding" Value="{Binding <SomePath>, TargetNullValue="True"}" />
</Style>

<DataGridCheckBoxColumn Style="{StaticResource DgCheckBoxColumnStyle}" />

For more information, please refer to the msdn. Should be easier than how you're trying to achieve it.

If you really want to access the controls in the columns you must provide one style for DataGridBoundColumn.ElementStyle and one for DataGridBoundColumn.EditingElementStyle.