[英]How do I databind Tooltips to a GridView bound to a DataTable?
Helo, 直升机,
I am trying to add Tooltips
to a GridView
bound to a DataTable
. 我试图将
Tooltips
添加到绑定到DataTable
的GridView
。 If I use a static text the tooltip works, so I assume that the error must lie in the data binding. 如果我使用静态文本工具提示工作,所以我假设错误必须在数据绑定中。
Here my XAML
: 在这里我的
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>
And here is my custom class TabModel
: 这是我的自定义类
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? 有人可以告诉我
Databinding
需要如何正确完成?
You can use an IMultiValueConverter . 您可以使用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>
Just wanted to post my working code for anyone with the same problem: 只是想为有相同问题的任何人发布我的工作代码:
XAML
: 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>
My Custom Class TabModel
: 我的自定义类
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; }
}
And the IMultiValueConverter
: 和
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();
}
}
Thanks to everyone who took their time and helped with this. 感谢所有花时间并帮助解决这个问题的人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.