【问题标题】:How to add tooltip with current DataGridTextColumn? WPF如何使用当前 DataGridTextColumn 添加工具提示? WPF
【发布时间】:2020-08-31 04:30:23
【问题描述】:

我已经看到了一些 SO 问题,但我似乎无法弄清楚如何使用我当前的设置来做到这一点。

我的 Datagrid 包含以下内容:

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>

我的数据网格中有一个 DataGridTextColumn

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

我的dataGridTextColumnLimit 在哪里:

    <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>

当我将鼠标悬停在单元格上时,我想以某种方式显示文本框的文本内容的工具提示。这可能/容易做到吗?

谢谢

【问题讨论】:

    标签: c# wpf xaml


    【解决方案1】:

    只需将 tooltip 属性绑定到单元格的数据上下文即可。

     <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>
    

    【讨论】:

      猜你喜欢
      • 2021-12-13
      • 2011-06-17
      • 2010-11-12
      • 2014-11-23
      • 2019-02-02
      • 1970-01-01
      • 2020-07-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多