0

DataGridTextColumnSilverlight 4にがDataGridあり、バインドされた値とは異なる列に ToolTip 値を設定したいと考えています。

テンプレート化された列を使用してこれを非常に簡単に実行できることはわかっていますが、余分な XAML が大量に追加され、読み取りと保守が面倒になります。

これは機能しますが、多くの余分なコードです - 特にテンプレートを変更する必要がある場合

    <data:DataGridTemplateColumn Header="Name" Width="*">
        <data:DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <TextBlock TextTrimming="WordEllipsis" Text="{Binding FullName}"
                 ToolTipService.ToolTip="{Binding Email}" Width="Auto" Margin="5" />
            </DataTemplate>
        </data:DataGridTemplateColumn.CellTemplate>
    </data:DataGridTemplateColumn>

スタイルまたは継承されたクラスでこれを行う良い方法を見つけたいと思います。私が言ったように、私の主な目標は、ツールチップのように些細なことのために、XAML の肥大化を可能な限り最善の方法で減らすことです。

thisthisのようなソリューションを含む同様のスタックオーバーフローの質問がいくつかありますが、どちらもセルの内容と同じツールチップ値を表示します (たとえば、オーバーフローした場合)。多くの場合、これはあなたが望むものですが、セルの内容に別のツールチップを表示しようとしています。

継承されたクラスのサンプル コード(スクロールして最後まで) を見つけました。これを変更しようとしましたが、XAML の知識が標準に達しておらず、これに一晩中費やしたくないため行き詰まりました。この特定の例は機能しているように見えますが、かなりのハックのように見えます。2 つの依存関係プロパティで機能するように変更しようとすると、さらに大きな問題になると思います。

PS。適切に作成されたサブクラスにより、TextTrimming などの他のプロパティも簡単にバインドできると期待しています。

4

1 に答える 1

0

このスタイルをリソース ディクショナリに追加してみてください。

<Style TargetType="{x:Type data:DataGridCell}">
    <Setter Property="ToolTip" Value="{Binding EMail}"/>
</Style>

コメントで要求したように、任意のプロパティへのバインディングを有効にするには、創造性を発揮する必要があります。私がしたことは、2 つの添付プロパティToolTipBindingIsToolTipBindingEnabled. は、セルのコンテンツを決定するプロパティとToolTipBinding同様に、ツールチップを決定するために列に設定され、上で述べたものと同様のスタイルを使用してオブジェクトに設定されます。BindingIsToolTipBindingEnabledtrueDataGridCell

次に、セルが読み込まれると、その親列からのバインディングがその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>
于 2009-12-31T07:02:24.033 に答える