开发者

Tooltip in Datagrid, added from code-behind

I'm new to wpf and im having trouble adding tooltips to my datagrid header from code-behind.

I need to use autogeneratecolumns on the datagrid which rules out xaml editing. All examples i could find involved som xaml editing.

I've tried to add it the DataGridTextColumn without succeding

private void dataGrid1_AutoGeneratedColumns(object sender, EventArgs e)
    {
        foreach (DataGridTextColumn item in dataGrid1.Columns)
        {
 开发者_运维知识库           ToolTip tooltip = new ToolTip();
            tooltip.Content = "Test";
            ToolTipService.SetToolTip(item, tooltip);
        }
    }

I can see that the ToolTip variable is set in the WPF visualizer but nothing shows in the datagrid.

Help!


I ended up using this approach:

    private void dataGrid1_AutoGeneratingColumn(object sender,DataGridAutoGeneratingColumnEventArgs e)
    {
            Style style = new Style(typeof(DataGridColumnHeader));
            Trigger trigger = new Trigger();
            trigger.Property = IsMouseOverProperty;
            trigger.Value = true;
            Setter setter = new Setter();
            setter.Property = ToolTipProperty;
            setter.Value = "Your tooltip";
            trigger.Setters.Add(setter);
            style.Triggers.Add(trigger);
            e.Column.HeaderStyle = style;

    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜