このスタイルをリソース ディクショナリに追加してみてください。
<Style TargetType="{x:Type data:DataGridCell}">
<Setter Property="ToolTip" Value="{Binding EMail}"/>
</Style>
コメントで要求したように、任意のプロパティへのバインディングを有効にするには、創造性を発揮する必要があります。私がしたことは、2 つの添付プロパティToolTipBinding
とIsToolTipBindingEnabled
. は、セルのコンテンツを決定するプロパティとToolTipBinding
同様に、ツールチップを決定するために列に設定され、上で述べたものと同様のスタイルを使用してオブジェクトに設定されます。Binding
IsToolTipBindingEnabled
true
DataGridCell
次に、セルが読み込まれると、その親列からのバインディングがそのToolTip
プロパティに適用されるようにコードを記述しました。
拡張クラスは次のとおりです。
public class DGExtensions
{
public static object GetToolTipBinding(DependencyObject obj)
{
return obj.GetValue(ToolTipBindingProperty);
}
public static void SetToolTipBinding(DependencyObject obj, object value)
{
obj.SetValue(ToolTipBindingProperty, value);
}
public static readonly DependencyProperty ToolTipBindingProperty =
DependencyProperty.RegisterAttached(
"ToolTipBinding",
typeof(object),
typeof(DGExtensions),
new FrameworkPropertyMetadata(null));
public static bool GetIsToolTipBindingEnabled(DependencyObject obj)
{
return (bool)obj.GetValue(IsToolTipBindingEnabled);
}
public static void SetIsToolTipBindingEnabled(
DependencyObject obj,
bool value)
{
obj.SetValue(IsToolTipBindingEnabled, value);
}
public static readonly DependencyProperty IsToolTipBindingEnabled =
DependencyProperty.RegisterAttached(
"IsToolTipBindingEnabled",
typeof(bool),
typeof(DGExtensions),
new UIPropertyMetadata(false, OnIsToolTipBindingEnabledChanged));
public static void OnIsToolTipBindingEnabledChanged(
DependencyObject d,
DependencyPropertyChangedEventArgs e)
{
DataGridCell cell = d as DataGridCell;
if (cell == null) return;
bool nv = (bool)e.NewValue, ov = (bool)e.OldValue;
// Act only on an actual change of property value.
if (nv == ov) return;
if (nv)
cell.Loaded += new RoutedEventHandler(cell_Loaded);
else
cell.Loaded -= cell_Loaded;
}
static void cell_Loaded(object sender, RoutedEventArgs e)
{
DataGridCell cell = sender as DataGridCell;
if (cell == null) return;
var binding = BindingOperations.GetBinding(
cell.Column, ToolTipBindingProperty);
if (binding == null) return;
cell.SetBinding(DataGridCell.ToolTipProperty, binding);
// This only gets called once, so remove the strong reference.
cell.Loaded -= cell_Loaded;
}
}
XAML の使用例:
<Window.Resources>
<Style TargetType="{x:Type tk:DataGridCell}">
<Setter
Property="dge:DGExtensions.IsToolTipBindingEnabled"
Value="True"/>
</Style>
</Window.Resources>
<Grid>
<tk:DataGrid ItemsSource="{Binding TheList}" AutoGenerateColumns="False">
<tk:DataGrid.Columns>
<tk:DataGridTextColumn
Header="PropA"
Binding="{Binding PropA}"
dge:DGExtensions.ToolTipBinding="{Binding PropB}"/>
<tk:DataGridTextColumn
Header="PropB"
Binding="{Binding PropB}"/>
</tk:DataGrid.Columns>
</tk:DataGrid>
</Grid>