DataGridCell Validation.ErrorTemplate ignored

假如想象 提交于 2019-12-30 18:33:41

问题


I'm trying to set the Validation.ErrorTemplate of the DataGridCells, here's the xaml code:

<Style x:Key="{x:Type DataGridCell}"  x:Uid="dataGridCellErrorTemplate" TargetType="{x:Type DataGridCell}">
    <Setter Property="Validation.ErrorTemplate">
        <Setter.Value>
            <ControlTemplate >
                <Border BorderBrush="Green" BorderThickness="2" ToolTip="Heidenei"></Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <!-- following line only for demonstration that the setter is working ... -->
    <Setter Property="Background" Value="Aquamarine"></Setter>              
</Style>

while the background of the datagridcells is successfully colored green (independant from any validation result) the used Validation.ErrorTemplate is still the default one, i.e. the red border.

I know there have been similar issues here in stackoverflow, e.g. Styling DataGridCell Error Template but they do not really solve my problem.

Any help is appreciated

Frank


回答1:


I believe that I'm experiencing the same issue.

When using a DataGridTemplateColumn the content is presented with a ContentPresenter. This content presenter uses the default error template.

I can't find a direct way to remove this template for an individual DataGridTemplateColumn but you can remove it for all content presenters in the DataGrid by adding a style to the DataGrid's resources.

<DataGrid.Resources>
    <Style TargetType="ContentPresenter">
        <Setter Property="Validation.ErrorTemplate" Value="{x:Null}"/>
    </Style>
</DataGrid.Resources>



回答2:


I had luck removing the irritating red border by using the following TextBlock style.

<Style TargetType="TextBlock">
    <Setter Property="Validation.ErrorTemplate" Value="{x:Null}"/>
</Style>


来源:https://stackoverflow.com/questions/9721960/datagridcell-validation-errortemplate-ignored

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!