【问题标题】:Add custom tooltip to row in DataGrid将自定义工具提示添加到 DataGrid 中的行
【发布时间】:2012-11-09 20:58:52
【问题描述】:

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

目前情况 - 显示单个选定行:

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

  • 我的 DataGrid 使用绑定到 ViewModel。
  • 使用适用于 Windows 桌面的 WPF 和 C#。

我真的不知道如何实现这一点,所以我愿意接受任何建议。

【问题讨论】:

    标签: c# wpf xaml datagrid


    【解决方案1】:

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

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

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

    【讨论】:

    • 但一个常见的问题是tooltip 正在退出window .. 你解决了吗??
    • 不,我不知道如何更改。对我来说,ToolTip 的这种行为可以部分显示在包含窗口之外。
    【解决方案2】:

    您可以使用RowDetailsTemplate

    这里是示例代码:

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

    【讨论】:

    • 请注意,DataGrid SelectionUnit 必须设置为 FullRow 才能工作。我最初将其设置为 Cell 并没有看到任何内容。
    • MoreInfo 属性必须从哪个对象可用?它是绑定到提供其行的 DataGrid 的同一个对象吗?
    • @void.pointer 如果您将ObservableCollection&lt;T&gt; 绑定到DataGrid ItemsSource,那么属性MoreInfo 应该在对象T 中定义。
    【解决方案3】:

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

    使用LodingRowEvent 并像这样添加您的工具提示:

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

    【讨论】:

      猜你喜欢
      • 2012-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-28
      相关资源
      最近更新 更多