我有DataGrid
DataGridTextColumn
,需要进行一些验证。
<DataGridTextColumn Header="Key" Width="100">
<DataGridTextColumn.Binding>
<Binding Path="Key">
<Binding.ValidationRules>
<local:DistinctValidation/>
</Binding.ValidationRules>
</Binding>
</DataGridTextColumn.Binding>
DataGrid
本身没有应用任何样式,但DataGridTextColumn
包含以下内容:
<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>
答案 0 :(得分:1)
您应该为要应用的模板的列的EditingElementStyle设置Validation.ErrorTemplate属性:
<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>