【问题标题】:Creating DataGrid column tooltip entirely in code behind完全在后面的代码中创建 DataGrid 列工具提示
【发布时间】:2012-04-13 21:50:03
【问题描述】:

我正在尝试完全在文件后面的代码中创建数据网格工具提示。 工具提示 XAML 代码如下所示:

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

我被困在&lt;Setter Property="ContentTemplate"&gt;。我当前的代码:

                Style style = new Style();
                style.TargetType = typeof(DataGridColumnHeader);
                Setter setter = new Setter();
                setter.Property = DependencyProperty.Register("ContentTemplate", typeof(DataTemplate), typeof(FrameworkElement), null);

任何人都可以向我展示一个在后面的代码中实现这部分的示例:

<Setter Property="ContentTemplate">
                        <Setter.Value>
                            <DataTemplate>
                                <ContentControl Content="{Binding}">
                                    <ToolTipService.ToolTip>
                                        <ToolTip Content="My Tooltip"></ToolTip>
                                    </ToolTipService.ToolTip>
                                </ContentControl>
                            </DataTemplate>
                        </Setter.Value>
                    </Setter>

谢谢!

【问题讨论】:

    标签: c# silverlight datagrid


    【解决方案1】:

    获得要添加工具提示的列的句柄后,请尝试以下操作。

    var style = new Style(typeof(DataGridColumnHeader));
    style.Setters.Add(new Setter(ToolTipService.ToolTipProperty, "Customer Name"));
    

    既然您已经定义了工具提示值,那么您可以像这样设置列的 HeaderStyle 属性...

    dgCustDetails.Columns[0].HeaderStyle = style;
    

    其中 dgCustDetails 是数据网格的名称。

    【讨论】:

      【解决方案2】:
      Style stylecell = new Style(typeof(DataGridCell));
      
      Binding descriptionbinding = new Binding("SRNO") { StringFormat = "{0}" };
      
      stylecell.Setters.Add(newSetter(ToolTipService.ToolTipProperty,descriptionbinding));
      
      gridItem.Columns[0].CellStyle = stylecell;
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-03-14
        • 2013-01-05
        • 1970-01-01
        • 2014-12-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多