开发者

Tooltip in Datagrid, added from code-behind

开发者 https://www.devze.com 2023-01-07 10:43 出处:网络
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

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

精彩评论

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