为一个列创建DataGrid提示(每行不同的提示)

3

我想为我的数据网格创建工具提示,但仅针对第一列,每行在该列上具有不同的工具提示文本。

由于我的数据网格可以显示不同类型的数据集(用户使用组合框选择正确的数据集),因此我认为应该在代码后台中创建工具提示(而不是使用某种绑定)?

更新:

XAML:

<DataGrid x:Name="DG_ConfigWindow" Height="253" Margin="0,37,0,0" VerticalAlignment="Top" Loaded="DG_ConfigWindow_Loaded" 
    CellEditEnding="DG_ConfigWindow_CellEditEnding" IsReadOnly="True" HorizontalAlignment="Left" Width="705" 
    PreviewKeyDown="DG_ConfigWindow_KeyDown" SelectionMode="Single" CanUserSortColumns="False" CanUserReorderColumns="False"> 
    <DataGrid.Resources>
        <Style TargetType="DataGridCell">
            <EventSetter Event="MouseEnter" Handler="DG_ConfigWindow_MouseEnter"/>
            <Setter Property="ToolTip" Value="{Binding Path=TooltipText, UpdateSourceTrigger=PropertyChanged, Mode=OneWay}"/>
        </Style>
    </DataGrid.Resources>
</DataGrid>

代码:

private string _TooltipText;
public string TooltipText
{
    get { return _TooltipText; }
    set
    {
        _TooltipText = value;
        NotifyPropertyChanged();
    }
}

private void DG_ConfigWindow_MouseEnter(object sender, MouseEventArgs e)
{
    TooltipText = "test";
}

INotifyPropertyChanged:

public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged([CallerMemberName] String propertyName = "")
{
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

我的类定义如下:

public partial class ConfigWindow : Window, INotifyPropertyChanged
{ ... }
1个回答

2
这个例子向你展示如何设置一个 ToolTip。在这种情况下,你可以从代码后端更新我称之为 YourText 的变量,并更改 ToolTip,以满足你的需要。请注意保留 HTML 标签。
<DataGrid.ToolTip>
    <ToolTip Background="#FAFAFA">
         <FlowDocumentScrollViewer ScrollViewer.VerticalScrollBarVisibility="Hidden" MaxWidth="250" >
             <FlowDocument FontSize="12" LineHeight="16" FontFamily="Sagoe UI" IsOptimalParagraphEnabled="True" IsHyphenationEnabled="True">
                  <Paragraph>
                     <Run Text="{Binding Path=YourText, UpdateSourceTrigger=PropertyChanged}" />
                   </Paragraph>
              </FlowDocument>
         </FlowDocumentScrollViewer>
    </ToolTip>
</DataGrid.ToolTip>

更新:

以下是在进行 MouseOver 操作时获取 DataGrid 元素的方法:

首先,在 Style 中添加一个 EventSetter,如下所示:

   <DataGrid.Resources>
         <Style TargetType="{x:Type DataGridCell}">
              <EventSetter Event="MouseEnter" Handler="EventSetter_OnHandler"/>
         </Style>
  </DataGrid.Resources>

并且Handler应该像这样:

private void EventSetter_OnHandler(object sender, MouseEventArgs e)

    {
        DataGridCell dgc = sender as DataGridCell;

        TextBox tb = Utils.GetChildOfType<TextBox>(dgc);
        //I assumed you have TextBox for your cell. After finding the element you then need to implement your logic here to update the YourText variable here.

    }

这个助手将帮助您找到您的手机

public static T GetChildOfType<T>(DependencyObject depObj) where T : DependencyObject
    {
        if (depObj == null) return null;

        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
        {
            var child = VisualTreeHelper.GetChild(depObj, i);

            var result = (child as T) ?? GetChildOfType<T>(child);
            if (result != null) return result;
        }
        return null;
    }

如果您的文本可以根据光标所在的行和列进行更新,那么这可能有效。这是可能的吗? - Alex
为此,您必须订阅鼠标悬停在数据网格单元格上,然后相应地更改我的示例中的“YourText”。 - Lion King
如何订阅?除了订阅之外,我还需要获取单元格的“坐标”(行和列索引)。 - Alex
谢谢。我已经能够获取单元格的行索引,但是我不知道如何获取目标列的索引。这是我缺少的最后一件事! - Alex
1
列索引比我想象的要容易获取:int columnIdx = dgc.Column.DisplayIndex; 感谢您的帮助! - Alex
显示剩余9条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接