Ichos Dichos
Ichos Dichos

Reputation: 51

DataGrid column validation error template

I've got a DataGrid with a DataGridTextColumn which needs some validation.

<DataGridTextColumn Header="Key" Width="100">
<DataGridTextColumn.Binding>
    <Binding Path="Key">
        <Binding.ValidationRules>
            <local:DistinctValidation/>
        </Binding.ValidationRules>
    </Binding>
</DataGridTextColumn.Binding>

The DataGrid itself has no style applied to it, but the DataGridTextColumn has the following one:

<Style TargetType="{x:Type DataGridTextColumn}">
<Setter Property="Validation.ErrorTemplate">
    <Setter.Value>
        <ControlTemplate>
            <StackPanel>
                <AdornedElementPlaceholder x:Name="placeholder" />
                <Popup HorizontalAlignment="Left" PopupAnimation="Fade" Placement="Bottom" IsOpen="true">
                    <TextBlock Text="{Binding [0].ErrorContent}"/>
                </Popup>
            </StackPanel>
        </ControlTemplate>
    </Setter.Value>
</Setter>

For simplicity I tried to set the IsOpen on the Popup always to true. Nevertheless the popup never shows up
Popup

Upvotes: 1

Views: 2545

Answers (1)

mm8
mm8

Reputation: 169160

You should set the Validation.ErrorTemplate property of the EditingElementStyle of the column for your template to get applied:

<DataGridTextColumn Binding="{Binding Test}">
  <DataGridTextColumn.EditingElementStyle>
    <Style TargetType="TextBox">
        <Setter Property="Validation.ErrorTemplate">
            <Setter.Value>
                <ControlTemplate>
                    <StackPanel>
                        <AdornedElementPlaceholder x:Name="placeholder" />
                        <Popup HorizontalAlignment="Left" PopupAnimation="Fade" Placement="Bottom" IsOpen="true">
                            <Grid Background="White">
                                <TextBlock Text="{Binding [0].ErrorContent}"/>
                            </Grid>
                        </Popup>
                    </StackPanel>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
  </DataGridTextColumn.EditingElementStyle>
</DataGridTextColumn>

Upvotes: 3

Related Questions