如何为数据网格标题添加工具提示,其中标题文本是动态生成的?

发布于 2024-10-10 11:53:56 字数 207 浏览 3 评论 0原文

我需要为 DataGrid (Silverlight 4) 的列标题添加工具提示。我将动态生成列数和列标题文本。

GridColumnCreation(....)
{
    IEnumerable allHeaderText = /* Linq query */; 
}

如何使用这个集合来设置工具提示?

I need to add a tooltip for a column header of a DataGrid (Silverlight 4). I will generate the number of columns and column header text dynamically.

GridColumnCreation(....)
{
    IEnumerable allHeaderText = /* Linq query */; 
}

How to use this collection to set a tooltip?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

伴随着你 2024-10-17 11:53:56

这可以比 @Farukh 的答案更简单:

<data:DataGridTextColumn.HeaderStyle>
  <Style TargetType="DataGridColumnHeader">
    <Setter Property="ToolTipService.ToolTipProperty"
            Value="Your tool tip here" />
  </Style>
</data:DataGridTextColumn.HeaderStyle>

或者,如果您需要在代码中执行此操作:

var style = new Style(typeof(DataGridColumnHeader));
style.Setters.Add(new Setter(ToolTipService.ToolTipProperty,
                             "Your tool tip here"));
column.HeaderStyle = style;

This can be done even more simply than in @Farukh's answer:

<data:DataGridTextColumn.HeaderStyle>
  <Style TargetType="DataGridColumnHeader">
    <Setter Property="ToolTipService.ToolTipProperty"
            Value="Your tool tip here" />
  </Style>
</data:DataGridTextColumn.HeaderStyle>

Or, if you need to do it in code:

var style = new Style(typeof(DataGridColumnHeader));
style.Setters.Add(new Setter(ToolTipService.ToolTipProperty,
                             "Your tool tip here"));
column.HeaderStyle = style;
软糖 2024-10-17 11:53:56

万一它可能对任何人有帮助。它在使用ToolTip属性时起作用。

<DataGridTextColumn.HeaderStyle>
    <Style TargetType="DataGridColumnHeader">
        <Setter Property="ToolTip" Value="{Binding}"/>
    </Style>
</DataGridTextColumn.HeaderStyle>

In case it might help anyone. It works when using ToolTip property.

<DataGridTextColumn.HeaderStyle>
    <Style TargetType="DataGridColumnHeader">
        <Setter Property="ToolTip" Value="{Binding}"/>
    </Style>
</DataGridTextColumn.HeaderStyle>
酷遇一生 2024-10-17 11:53:56

如果您不想为标题创建新样式,只需为列标题添加 TextBlock 并在其上设置工具提示即可。

<DataGridTextColumn>
    <DataGridTextColumn.Header>
        <TextBlock Text="ColumnA" ToolTip="ColumnA Tooltip"/>
    </DataGridTextColumn.Header>
</DataGridTextColumn>

If you do not want to create a new style for the Header, simply add a TextBlock for your column header and set the tooltip on it.

<DataGridTextColumn>
    <DataGridTextColumn.Header>
        <TextBlock Text="ColumnA" ToolTip="ColumnA Tooltip"/>
    </DataGridTextColumn.Header>
</DataGridTextColumn>
心作怪 2024-10-17 11:53:56

这可以通过使用 DataGridTextColumn & 来完成。 DataGridTextColumn.HeaderStyle。在 headerstyle 标记中,使用 ToolTipService 并将内容绑定到生成的动态值。这是一个示例代码......

        <data:DataGrid.Columns>
            <data:DataGridTextColumn Header="First Name" Binding="{Binding FName}" >
                <data:DataGridTextColumn.HeaderStyle>
                    <Style TargetType="dataprimitives:DataGridColumnHeader">
                        <Setter Property="ContentTemplate">
                            <Setter.Value>
                                <DataTemplate>
                                    <ContentControl Content="{Binding}">
                                        <ToolTipService.ToolTip>
                                            <ToolTip Content="Tooltip First" />
                                        </ToolTipService.ToolTip>
                                    </ContentControl>
                                </DataTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </data:DataGridTextColumn.HeaderStyle>
            </data:DataGridTextColumn>

            <data:DataGridTextColumn Header="Last Name" Binding="{Binding LName}">
                <data:DataGridTextColumn.HeaderStyle>
                    <Style TargetType="dataprimitives:DataGridColumnHeader">
                        <Setter Property="ContentTemplate">
                            <Setter.Value>
                                <DataTemplate>
                                    <ContentControl Content="{Binding}">
                                        <ToolTipService.ToolTip>
                                            <ToolTip Content="Tooltip Second"></ToolTip>
                                        </ToolTipService.ToolTip>
                                    </ContentControl>
                                </DataTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </data:DataGridTextColumn.HeaderStyle>
            </data:DataGridTextColumn>

            <data:DataGridTextColumn Header="City" Binding="{Binding City}">
                <data:DataGridTextColumn.HeaderStyle>
                    <Style TargetType="dataprimitives:DataGridColumnHeader">
                        <Setter Property="ContentTemplate">
                            <Setter.Value>
                                <DataTemplate>
                                    <ContentControl Content="{Binding}">
                                        <ToolTipService.ToolTip>
                                            <ToolTip Content="Tooltip Third"></ToolTip>
                                        </ToolTipService.ToolTip>
                                    </ContentControl>
                                </DataTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </data:DataGridTextColumn.HeaderStyle>
            </data:DataGridTextColumn>
        </data:DataGrid.Columns>

    </data:DataGrid>

</Grid>

其中 Custdetails.. 是这样的..

class Customer
{
    public string LName { set; get; }
    public string FName { set; get; }
    public string City { set; get; }
}

DataBinding...

List<Customer> customers = new List<Customer>
                                    {
                                        new Customer { LName="Alan", FName="Ameen", City="New York" },
                                        new Customer { LName="Forgeard", FName="Steven", City="Mumbai" },
                                        new Customer { LName="Angur", FName="Paul", City="São Paulo" }
                                    };
dgCustDetails.ItemsSource = customers;

这将显示标题工具提示... 使其动态.. 将 ToolTip 内容替换为 Binding &想要的值...

This can be done by using DataGridTextColumn & DataGridTextColumn.HeaderStyle. In the headerstyle tag, use the ToolTipService and bind the content to the dynamic values generated. Here's a sample code for this...

        <data:DataGrid.Columns>
            <data:DataGridTextColumn Header="First Name" Binding="{Binding FName}" >
                <data:DataGridTextColumn.HeaderStyle>
                    <Style TargetType="dataprimitives:DataGridColumnHeader">
                        <Setter Property="ContentTemplate">
                            <Setter.Value>
                                <DataTemplate>
                                    <ContentControl Content="{Binding}">
                                        <ToolTipService.ToolTip>
                                            <ToolTip Content="Tooltip First" />
                                        </ToolTipService.ToolTip>
                                    </ContentControl>
                                </DataTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </data:DataGridTextColumn.HeaderStyle>
            </data:DataGridTextColumn>

            <data:DataGridTextColumn Header="Last Name" Binding="{Binding LName}">
                <data:DataGridTextColumn.HeaderStyle>
                    <Style TargetType="dataprimitives:DataGridColumnHeader">
                        <Setter Property="ContentTemplate">
                            <Setter.Value>
                                <DataTemplate>
                                    <ContentControl Content="{Binding}">
                                        <ToolTipService.ToolTip>
                                            <ToolTip Content="Tooltip Second"></ToolTip>
                                        </ToolTipService.ToolTip>
                                    </ContentControl>
                                </DataTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </data:DataGridTextColumn.HeaderStyle>
            </data:DataGridTextColumn>

            <data:DataGridTextColumn Header="City" Binding="{Binding City}">
                <data:DataGridTextColumn.HeaderStyle>
                    <Style TargetType="dataprimitives:DataGridColumnHeader">
                        <Setter Property="ContentTemplate">
                            <Setter.Value>
                                <DataTemplate>
                                    <ContentControl Content="{Binding}">
                                        <ToolTipService.ToolTip>
                                            <ToolTip Content="Tooltip Third"></ToolTip>
                                        </ToolTipService.ToolTip>
                                    </ContentControl>
                                </DataTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </data:DataGridTextColumn.HeaderStyle>
            </data:DataGridTextColumn>
        </data:DataGrid.Columns>

    </data:DataGrid>

</Grid>

where Custdetails.. is something like this..

class Customer
{
    public string LName { set; get; }
    public string FName { set; get; }
    public string City { set; get; }
}

DataBinding...

List<Customer> customers = new List<Customer>
                                    {
                                        new Customer { LName="Alan", FName="Ameen", City="New York" },
                                        new Customer { LName="Forgeard", FName="Steven", City="Mumbai" },
                                        new Customer { LName="Angur", FName="Paul", City="São Paulo" }
                                    };
dgCustDetails.ItemsSource = customers;

This would display the header tooltips... To make it dynamic.. Replace the ToolTip Content with Binding & the desired value...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文