简体   繁体   中英

How to add tooltip with current DataGridTextColumn? WPF

I've seen a few SO questions but I can't seem to figure out how to do this with my current setup.

My Datagrid contains this:

CellStyle="{StaticResource DataGridContentCellCentering}"

<Style x:Key="DataGridContentCellCentering" TargetType="{x:Type DataGridCell}">
    <Setter Property="Background" Value="{StaticResource PColour}"/>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridCell}">
                <Grid Background="{TemplateBinding Background}">
                    <ContentPresenter VerticalAlignment="Center" />
                    <ContentPresenter HorizontalAlignment="Center"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="Background" Value="{StaticResource PColour}"/>
    <Setter Property="TextBlock.TextAlignment" Value="Center"/>
    <Setter Property="MinHeight" Value="15"/>
</Style>

I have a DataGridTextColumn in my datagrid

<DataGridTextColumn Header="{x:Static p:Resources.Sabel}" MinWidth="120"
    x:Name="SName" Binding="{Binding SName}" Width="*"
    EditingElementStyle="{StaticResource dataGridTextColumnLimit}"/>

Where my dataGridTextColumnLimit is this:

    <Style x:Key="dataGridTextColumnLimit" TargetType="{x:Type TextBox}">
        <Setter Property="Foreground" Value="{StaticResource Balour}"/>
        <Setter Property="MaxLength" Value="20"/>
        <Setter Property="TextWrapping" Value="Wrap" />
        <Setter Property="TextAlignment" Value="Center"/>
    </Style>

I want to somehow have a tooltip that shows the text content of the textbox when i hover over the cell. Is this possible/easy to do?

Thanks

Just bind the tooltip property to the data context of the cell.

 <Style x:Key="DataGridContentCellCentering" TargetType="{x:Type DataGridCell}">
        <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Mode=Self},Path=DataContext}"/>
        <Setter Property="Background" Value="{StaticResource PColour}"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type DataGridCell}">
                    <Grid Background="{TemplateBinding Background}">
                        <ContentPresenter VerticalAlignment="Center" />
                        <ContentPresenter HorizontalAlignment="Center"/>
                    </Grid>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
        <Setter Property="Background" Value="{StaticResource PColour}"/>
        <Setter Property="TextBlock.TextAlignment" Value="Center"/>
        <Setter Property="MinHeight" Value="15"/>
 </Style>

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