简体   繁体   中英

IErrorDataInfo apply tooltip style to all DataGridTextColumns

I'm working in a WPF C# project, I had a lot of problems showing tooltips for validation purposes and I came across this solution in another post here on StackOverflow.

<DataGridTextColumn MinWidth="80" ...>
    <DataGridTextColumn.ElementStyle>
    <Style TargetType="{x:Type TextBlock}">
            <Style.Triggers>
                <Trigger Property="Validation.HasError" Value="true">
                    <Setter Property="ToolTip"
                Value="{Binding RelativeSource={RelativeSource Self}, 
                       Path=(Validation.Errors)[0].ErrorContent}"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </DataGridTextColumn.ElementStyle>
</DataGridTextColumn>

and though this works and applies the tooltip style to the columns, I have to paste it in all the columns

I want it to happen... I tried to create a style already but I was unsuccessful. I think it as something to do with the bindings and the "relative source" thing.

Is it possible to create a style out of this and apply it to every column, how do I do it?!

I just figured out how to make it work!

               <DataGrid.Resources>
                    <Style x:Key="DataGridTextColumnError" TargetType="{x:Type TextBlock}">
                        <Style.Triggers>
                            <Trigger Property="Validation.HasError" Value="true">
                                <Setter Property="ToolTip" Value="{Binding RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors)[0].ErrorContent}" />
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </DataGrid.Resources>

The value binding needed to be changed to static. Hope this helps someone!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM