在自定义DataGridTextColumn上显示ToolTip DependencyProperty

时间:2014-09-10 18:07:45

标签: wpf wpfdatagrid

我知道我可以设置列的样式并以这种方式设置工具提示,但我希望在网格列上有一个真正的工具提示属性。 我在创建文本块之前暴露工具提示属性时遇到问题。 这是我的自定义列。

public class DataGridTextColumn : System.Windows.Controls.DataGridTextColumn
{
    public static readonly DependencyProperty TextTrimmingProperty = DependencyProperty.Register(
        "TextTrimming", typeof (TextTrimming), typeof (DataGridTextColumn),
        new PropertyMetadata(default(TextTrimming)));

    /// <summary>
    ///     HorizontalAlignment Dependency Property.
    /// </summary>
    public static readonly DependencyProperty HorizontalAlignmentProperty =
        DependencyProperty.Register(
            "HorizontalAlignment",
            typeof (HorizontalAlignment),
            typeof (DataGridTextColumn),
            new FrameworkPropertyMetadata(HorizontalAlignment.Left, FrameworkPropertyMetadataOptions.AffectsArrange), ValidateHorizontalAlignmentValue);

    public DataGridTextColumn()
    {
        ElementStyle = Application.Current.Resources["DataGridTextStyle"] as Style;
        EditingElementStyle = Application.Current.Resources["DataGridEditingTextStyle"] as Style;
    }

    /// <summary>
    ///     TextTrimming, only works in read-only mode
    /// </summary>
    public TextTrimming TextTrimming
    {
        get { return (TextTrimming) GetValue(TextTrimmingProperty); }
        set { SetValue(TextTrimmingProperty, value); }
    }

    /// <summary>
    ///     HorizontalAlignment Property.
    /// </summary>
    public HorizontalAlignment HorizontalAlignment
    {
        get { return (HorizontalAlignment) GetValue(HorizontalAlignmentProperty); }
        set { SetValue(HorizontalAlignmentProperty, value); }
    }

    internal static bool ValidateHorizontalAlignmentValue(object value)
    {
        var ha = (HorizontalAlignment) value;
        return (ha == HorizontalAlignment.Left
                || ha == HorizontalAlignment.Center
                || ha == HorizontalAlignment.Right
                || ha == HorizontalAlignment.Stretch);
    }

    protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
    {
        var element = (TextBlock) base.GenerateElement(cell, dataItem);
        element.TextTrimming = TextTrimming;
        element.HorizontalAlignment = HorizontalAlignment;
        return element;
    }

工具提示必须附加到文本块。

2 个答案:

答案 0 :(得分:0)

这是你想要的:

element.ToolTip = new ToolTip { Content = new TextBlock { Text = "Da Tooltip!" } };

?...

答案 1 :(得分:0)

我自己回答这个问题,因为我认为我的发现非常有帮助。 我需要一种方法将工具提示绑定到DataGridTextColumn,所以我反过来设计了Binding属性。这是我的工具提示代码:

public BindingBase ToolTip
    {
        get { return _toolTip; }
        set
        {
            if (_toolTip == value) return;
            _toolTip = value;
            NotifyPropertyChanged("ToolTip");

        }
    }

覆盖GenerateElement

protected override FrameworkElement GenerateElement(DataGridCell cell, object dataItem)
    {
        var element = (TextBlock) base.GenerateElement(cell, dataItem);
        element.TextTrimming = TextTrimming;
        element.HorizontalAlignment = HorizontalAlignment;
        ApplyToolTipBinding(element, FrameworkElement.ToolTipProperty);
        return element;
    }

其余代码可以在this gist找到。我已经能够通过用我的自定义列替换模板列来删除许多米的XAML代码。您可以直接在DataGridTextColumn上设置ToolTip,TextTrimming和Horizo​​ntalAlignment。

我希望你能找到有用的代码。