c# - 仅为一列创建 DataGrid 工具提示(每行不同的工具提示)

标签 c# wpf datagrid

我想为我的数据网格创建工具提示,但仅限于第一列,列中的每一行都有不同的工具提示文本。

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

更新:

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
{ ... }

最佳答案

此示例向您展示了如何设置ToolTip。在这种情况下,根据您的需要,您可以从代码隐藏更新我称为 YourText 的变量并更改 ToolTip

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

    }

这个 helper 会帮你找到你的手机

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

关于c# - 仅为一列创建 DataGrid 工具提示(每行不同的工具提示),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52381158/

相关文章:

c# - 搜索中的单词如何像在 Google 中一样附加

wpf - 什么类型的应用程序启动更快 : Windows Forms or WPF?

c# - 默认数据上下文

c# - Datagrid:如何获取 SelectedItem 的 CurrentCell?

c# - DataGrid 显示内容值为 'System.Data.DataRowView'

reactjs - Material UI 的 GridToolbarExport 在上一版本后停止工作? 5.0.0-alpha.37

c# - 工厂模式中使用的策略模式?

c# - System.Convert 如何适应 OO 约定?

c# - 如何使用 C# 以编程方式在 OpenFile 对话框中选择文件

c# - 使用数组变量绑定(bind)对象