Search code examples
c#wpfdatagrid

Datagridtextcolumn border if the text is empty


I have a datagrid with a datagridtextcolumn. I want to show a red border if the text of that column is empty.

If I use DataGridCell as TargetType for my Style, i can set the properties for the border. But the DataGridCell does not have a text property which i can use to trigger the style. I also tried to use Content, but this property does also not exist:

<DataGridTextColumn Header="MyValue" Binding="{Binding MyValue, UpdateSourceTrigger=PropertyChanged}" IsReadOnly="False">
    <DataGridTextColumn.CellStyle>
        <Style TargetType="DataGridCell">
            <Style.Triggers>
                <Trigger Property="???" Value="">
                    <Setter Property="BorderBrush" Value="Red"/>
                    <Setter Property="BorderThickness" Value="1 1 1 1"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </DataGridTextColumn.CellStyle>
</DataGridTextColumn>

If I use TextBlock as TargetType for my Style, i can use the Property Text to trigger the style. But the TextBlock does not have the Properties for Borders to set a border style:

<DataGridTextColumn Header="MyValue" Binding="{Binding MyValue, UpdateSourceTrigger=PropertyChanged}" IsReadOnly="False">
    <DataGridTextColumn.CellStyle>
        <Style TargetType="TextBlock">
            <Style.Triggers>
                <Trigger Property="Text" Value="">
                    <Setter Property="???" Value="Red"/>
                    <Setter Property="???" Value="1 1 1 1"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </DataGridTextColumn.CellStyle>
</DataGridTextColumn>

How can i achive a red border if the datagridtextcolumn is empty?


Solution

  • If you want a red border to appear when the text of a DataGridTextColumn is empty, you can use CellTemplate and use Datatrigger for Border and bind it to text. The code for your XAML has been updated:

    <DataGrid ItemsSource="{Binding Path=MyProperty}" AutoGenerateColumns="False" CanUserAddRows="False">
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="MyValue" IsReadOnly="False">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Border BorderThickness="2">
                                <TextBox x:Name="MyText" Text="{Binding MyValue, UpdateSourceTrigger=PropertyChanged}"/>
                                <Border.Style>
                                    <Style TargetType="Border">
                                        <Setter Property="BorderBrush" Value="Transparent"/>
                                        <Style.Triggers>
                                            <DataTrigger Binding="{Binding  ElementName=MyText, Path=Text}"  Value="">
                                                <Setter Property="BorderBrush" Value="Red"/>
                                            </DataTrigger>
                                        </Style.Triggers>
                                    </Style>
                                </Border.Style>
                            </Border>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGridTextColumn>
        </DataGrid.Columns>