wpfbindingdatagriddatagridcolumn

Add two items in DataGrid column in WPF


I want to have a CheckBox and a TextBlock together in a column of DataGrid like this:

<DataGrid CanUserAddRows="False" CanUserDeleteRows="False" >
    <DataGrid.Columns>
        <DataGridTemplateColumn Header="one">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock x:Name="textBlock1"></TextBlock>
                        <CheckBox x:Name="checkBox1"></CheckBox>
                    </StackPanel>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

And I want to bind data from database to this column: My data in database field is like that: "0-Hello" or "1-Bye"

If field was "0-hello" then checkBox1 should be unchecked and textBlock1 should be equal to "hello".

If field was "1-bye" then checkBox1 should be checked and textBlock1 should be equal to "hello".

How can I do that?


Solution

  • Use a Datatrigger to set the checkbox and a Converter to get the substring that you wan't to view

    <DataGrid  CanUserAddRows="False" CanUserDeleteRows="False" ItemsSource="{Binding Items}">
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="one" >
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel >
                                <TextBlock x:Name="textBlock1" Text="{Binding YourProperty,Converter={StaticResource GetSubStringConverter}}"></TextBlock>
                                <CheckBox x:Name="checkBox1" >
                                    <CheckBox.Style>
                                        <Style TargetType="CheckBox">
                                            <Setter Property="IsChecked" Value="True"/>
                                            <Style.Triggers>
                                                <DataTrigger Binding="{Binding YourProperty[0]}" Value="0">
                                                    <Setter Property="IsChecked" Value="False"></Setter>
                                                </DataTrigger>
                                            </Style.Triggers>
                                        </Style>
                                    </CheckBox.Style>
                                </CheckBox>
                            </StackPanel>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>
    

    the converter will simply extract the text to display

     public class GetSubStringConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value == null) return null;
            return value.ToString().Substring(2);
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }