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

时间:2018-09-18 07:29:33

标签: c# wpf datagrid

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

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

更新

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 个答案:

答案 0 :(得分:1)

此示例说明如何设置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>

更新:

这是一种在执行DataGrid时获取MouseOver元素的方法

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

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

Handler应该是这样的:

私有无效EventSetter_OnHandler(对象发送者,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;
    }