如何将工具提示数据绑定到绑定到DataTable的GridView?

时间:2016-06-13 13:24:05

标签: c# wpf xaml data-binding

Helo,

我正在尝试将Tooltips添加到与GridView绑定的DataTable。 如果我使用静态文本工具提示工作,所以我假设错误必须在数据绑定中。

这是我的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;}
        }

有人可以告诉我Databinding需要如何正确完成?

2 个答案:

答案 0 :(得分:0)

您可以使用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>

答案 1 :(得分:0)

只想为遇到同样问题的人发布我的工作代码:

XAML

<Window.Resources>
    <local:IndexToDescriptionConverter x:Key="IndexToDescriptionConverter" />
</Window.Resources> 
....
    <TabControl x:Name="MainTabs" Grid.Row="2" 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">
                                        <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>
                </DataGrid>
            </DataTemplate>
        </TabControl.ContentTemplate>
    </TabControl>

我的自定义班级TabModel

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

IMultiValueConverter

public class IndexToDescriptionConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
    {
        int index = (int)values[0];
        if(index < 0)
        {
            return null;
        }
        ObservableCollection<String> ColumnDescriptions = (ObservableCollection<String>)values[1];
        return ColumnDescriptions.ElementAt(index);
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

感谢所有花时间并为此提供帮助的人。