DataGridCell的模板化工具提示取决于单元格的内容

时间:2017-06-06 05:32:59

标签: wpf datagrid tooltip

我想根据DataGridCell的内容显示不同视图的工具提示。以下代码有效。

<DataGrid>
    <DataGrid.Columns>
        <DataGridTextColumn Header="1" Binding="{Binding}">
            <DataGridTextColumn.CellStyle>
                <Style TargetType="{x:Type DataGridCell}">
                    <Setter Property="ToolTip">
                        <Setter.Value>
                            <ToolTip>
                                <ToolTip.Content>
                                    <TextBlock Foreground="Blue" Text="{Binding}"></TextBlock>
                                </ToolTip.Content>
                            </ToolTip>
                        </Setter.Value>
                    </Setter>
                </Style>
            </DataGridTextColumn.CellStyle>
        </DataGridTextColumn>
    </DataGrid.Columns>
    <DataGrid.Items>
        <system:String>AAA</system:String>
        <system:Int32>2</system:Int32>
    </DataGrid.Items>
</DataGrid>

但是当我尝试使用模板时,我没有成功(我想根据视图模型的类型选择模板)。

<DataGrid>
    <DataGrid.Columns>
        <DataGridTextColumn Header="1" Binding="{Binding}">
            <DataGridTextColumn.CellStyle>
                <Style TargetType="{x:Type DataGridCell}">
                    <Setter Property="ToolTip">
                        <Setter.Value>
                            <ToolTip>
                                <ToolTip.ContentTemplate>
                                    <DataTemplate>
                                        <ContentControl Content="{Binding}">
                                            <ContentControl.Resources>
                                                <DataTemplate DataType="system:String">
                                                    <TextBlock Foreground="Blue" Text="{Binding}"></TextBlock>
                                                </DataTemplate>
                                                <DataTemplate DataType="system:Int32">
                                                    <TextBlock Foreground="Red" Text="{Binding}"></TextBlock>
                                                </DataTemplate>
                                            </ContentControl.Resources>
                                        </ContentControl>
                                    </DataTemplate>
                                </ToolTip.ContentTemplate>
                            </ToolTip>
                        </Setter.Value>
                    </Setter>
                </Style>
            </DataGridTextColumn.CellStyle>
        </DataGridTextColumn>
    </DataGrid.Columns>
    <DataGrid.Items>
        <system:String>AAA</system:String>
        <system:Int32>2</system:Int32>
    </DataGrid.Items>
</DataGrid>

如何解决?

1 个答案:

答案 0 :(得分:1)

Content本身的Tooltip属性绑定到DataContext,并使用{x:Type}使用显式类型规范:

<DataGrid>
    <DataGrid.Columns>
        <DataGridTextColumn Header="1" Binding="{Binding}">
            <DataGridTextColumn.CellStyle>
                <Style TargetType="{x:Type DataGridCell}">
                    <Setter Property="ToolTip">
                        <Setter.Value>
                            <ToolTip Content="{Binding}">
                                <ToolTip.ContentTemplate>
                                    <DataTemplate>
                                        <ContentControl Content="{Binding}">
                                            <ContentControl.Resources>
                                                <DataTemplate DataType="{x:Type system:String}">
                                                    <TextBlock Foreground="Blue" Text="{Binding}"></TextBlock>
                                                </DataTemplate>
                                                <DataTemplate DataType="{x:Type system:Int32}">
                                                    <TextBlock Foreground="Red" Text="{Binding}"></TextBlock>
                                                </DataTemplate>
                                            </ContentControl.Resources>
                                        </ContentControl>
                                    </DataTemplate>
                                </ToolTip.ContentTemplate>
                            </ToolTip>
                        </Setter.Value>
                    </Setter>
                </Style>
            </DataGridTextColumn.CellStyle>
        </DataGridTextColumn>
    </DataGrid.Columns>
    <DataGrid.Items>
        <system:String>AAA</system:String>
        <system:Int32>2</system:Int32>
    </DataGrid.Items>
</DataGrid>