首页 > 解决方案 > 定义 DataGridTextColumn 工具提示的样式

问题描述

我正在尝试更改 DataGridTextColumn 工具提示的样式,但是下面的 xaml 代码不起作用。当我运行应用程序时,会显示自定义工具提示并在其中写入 System.Windows.Style。该按钮也未显示。我哪里错了?

<DataGrid.Columns>
            <DataGridTextColumn  Width="*" Binding="{Binding W_NAME}" Header="Name">
                <DataGridTextColumn.CellStyle>
                    <Style TargetType="DataGridCell">
                        <Setter Property="ToolTip">
                            <Setter.Value>
                                <Style TargetType="ToolTip">
                                    <Setter Property="OverridesDefaultStyle" Value="True"/>
                                    <Setter Property="HasDropShadow" Value="True"/>
                                    <Setter Property="HorizontalAlignment" Value="Left"/>
                                    <Setter Property="Content" Value="mytext"/>
                                    <Setter Property="VerticalAlignment" Value="Center"/>
                                    <Setter Property="Template">
                                        <Setter.Value>
                                            <ControlTemplate TargetType="ToolTip">
                                                <Border Name="RootElement" CornerRadius="2,2,0,0">
                                                    <Border.Background>
                                                        <SolidColorBrush x:Name="BorderBrush" Color="Black"/>
                                                    </Border.Background>
                                                    <Grid Margin="4" Background="{TemplateBinding Background}">
                                                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
                                                                          VerticalAlignment="{TemplateBinding VerticalAlignment}"
                                                                          TextElement.FontFamily="{DynamicResource Glaho}"
                                                                          Content="{TemplateBinding Content}"
                                                                          Margin="4,5,4,4">

                                                        </ContentPresenter>
                                                        <Button Content="This is button" HorizontalAlignment="Right" Command="{Binding somecommand}"/>
                                                    </Grid>
                                                </Border>
                                            </ControlTemplate>
                                        </Setter.Value>
                                    </Setter>
                                </Style>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </DataGridTextColumn.CellStyle>
            </DataGridTextColumn>

标签: wpftooltipwpf-controls

解决方案


您非常接近,问题是您已将其设置Style为 ToolTip 本身:

<Style TargetType="DataGridCell">
    <Setter Property="ToolTip">
        <Setter.Value>
            <Style TargetType="ToolTip"> <-------------- badness!

您需要做的是将 ToolTip 控件设置为 ToolTip,然后在其上设置 Style:

<Style TargetType="DataGridCell">
    <Setter Property="ToolTip">
        <Setter.Value>
            <ToolTip>
                <ToolTip.Style>
                    <Style TargetType="ToolTip">

推荐阅读