如何将ToolTips的DataTip数据绑定到绑定到DataTable的GridView? [英] How do I databind Tooltips to a GridView bound to a DataTable?

查看:161
本文介绍了如何将ToolTips的DataTip数据绑定到绑定到DataTable的GridView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Helo,

我正在尝试添加工具提示 GridView 绑定到一个 DataTable
如果我使用静态文本工具提示工作,那么我假设错误必须在数据绑定中。

I am trying to add Tooltips to a GridView bound to a DataTable. If I use a static text the tooltip works, so I assume that the error must lie in the data binding.

这里我的 XAML

   <TabControl x:Name="MainTabs" ItemsSource="{Binding Path=TabModels}" DisplayMemberPath="TabCaption">
        <TabControl.ContentTemplate>
            <DataTemplate>
                <DataGrid ItemsSource="{Binding Path=TabDataTable}" AutoGenerateColumns="True" IsReadOnly="True" SelectionUnit="CellOrRowHeader" >

                    <DataGrid.Resources>
                        <Style TargetType="{x:Type DataGridColumnHeader}">
                            <Style.Triggers>
                                <Trigger Property="IsMouseOver" Value="True">
                                    <Setter Property="ToolTip" Value="{Binding Path=ColumnDescriptions, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type DataTemplate}}}"/>
                                </Trigger>
                            </Style.Triggers>
                        </Style>
                    </DataGrid.Resources> 

                </DataGrid>
            </DataTemplate>
        </TabControl.ContentTemplate>
    </TabControl>

这里是我的自定义类 TabModel

public ObservableCollection<TabModel> TabModels { get; set; }    
public class TabModel
        {
            public string Title { get; set; }
            public string TabCaption { get; set; }
            public DataTable TabDataTable { get; set; }
            public ObservableCollection<String> ColumnDescriptions { get; set;}
        }

有人可以告诉我如何数据绑定需要正确完成?

Could someone please tell me how the Databinding needs to be done correctly?

推荐答案

您可以使用IMultiValueConverter

<DataGrid.Resources>
    <Style TargetType="{x:Type DataGridColumnHeader}">
        <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="ToolTip">
            <Setter.Value>
                <MultiBinding Converter="{StaticResource IndexToDescriptionConverter}">
                    <Binding Path="Column.DisplayIndex" 
                             RelativeSource="{RelativeSource Self}"/>
                    <Binding Path="DataContext.ColumnDescriptions" 
                             RelativeSource="{RelativeSource FindAncestor, AncestorType={x:Type DataGrid}}"/>
                </MultiBinding>
            </Setter.Value>
            </Setter>
        </Trigger>
        </Style.Triggers>
    </Style>
</DataGrid.Resources>

这篇关于如何将ToolTips的DataTip数据绑定到绑定到DataTable的GridView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆