wpf 将自定义工具提示添加到 DataGrid 中的行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13496142/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:19:06  来源:igfitidea点击:

Add custom tooltip to row in DataGrid

c#wpfxamldatagrid

提问by Drahcir

I would like to customize my DataGrid to show a tooltip within the selected row, please see the mockup images below for a better idea of what I want to achieve.

我想自定义我的 DataGrid 以在所选行中显示工具提示,请查看下面的模型图片以更好地了解我想要实现的目标。

As it is at the moment- Shows a single selected row: enter image description here

目前- 显示单个选定行: 在此处输入图片说明

How I would like- Shows the same row selected, now with tooltip:

我想要的- 显示选择的同一行,现在带有工具提示:

enter image description here

在此处输入图片说明

  • My DataGrid uses Binding to the ViewModel.
  • Working with WPF & C# for Windows desktop.
  • 我的 DataGrid 使用绑定到 ViewModel。
  • 在 Windows 桌面上使用 WPF 和 C#。

I don't really have any idea how to achieve this, so I'm open to any suggestions at all.

我真的不知道如何实现这一目标,所以我对任何建议都持开放态度。

回答by Martin

I use the DataGrid.RowStyleto set the tooltip.

我使用DataGrid.RowStyle来设置工具提示。

My bound objects have a ToolTipTextproperty which contains the content of the ToolTip.

我的绑定对象有一个ToolTipText属性,其中包含ToolTip.

<DataGrid.RowStyle>
    <Style TargetType="DataGridRow">
        <Setter Property="ToolTip">
            <Setter.Value>
                <TextBlock Text="{Binding ToolTipText}" />
            </Setter.Value>
        </Setter>
    </Style>
</DataGrid.RowStyle>

回答by kmatyaszek

You can use RowDetailsTemplate.

您可以使用RowDetailsTemplate

Here is sample code:

这是示例代码:

<DataGrid Name="grid" AutoGenerateColumns="False">
    <DataGrid.RowDetailsTemplate>
        <DataTemplate>
            <TextBlock Background="Orange" Text="{Binding MoreInfo}" TextWrapping="Wrap"
                       HorizontalAlignment="Stretch" VerticalAlignment="Stretch" />
        </DataTemplate>
    </DataGrid.RowDetailsTemplate>
    <DataGrid.Columns>
        <DataGridTextColumn Header="ID" Binding="{Binding ID}" />
        <DataGridTextColumn Header="ID" Binding="{Binding Name}" />
        <DataGridTextColumn Header="ID" Binding="{Binding Surname}" />
    </DataGrid.Columns>
</DataGrid>

回答by FlyingOliver

Another simple way to add a tooltip on a row in a datagrid is the following.

在数据网格中的行上添加工具提示的另一种简单方法如下。

Use the LodingRowEvent and add your tooltip like this:

使用LodingRow事件并添加您的工具提示,如下所示:

private void grdItemlogs_LoadingRow(object sender, DataGridRowEventArgs e)
    {
        if (e.Row != null)
        {
            string toolTipText = "Your Tooltip string content"
            e.Row.ToolTip = toolTipText;

        }
    }