2012-03-30 64 views
2

我想完全在代码后面的文件中创建datagrid工具提示。 工具提示XAML代码如下所示:完全在后面的代码中创建DataGrid列工具提示

<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> 

我被困在<Setter Property="ContentTemplate">。我当前的代码:

   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> 

谢谢!

回答

3

一旦你得到了你想要添加工具提示的列的句柄,那么你在下面尝试。

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

现在您已经定义了你提示值,那么你可以设置的列这样的事情HeaderStyle属性...

dgCustDetails.Columns[0].HeaderStyle = style; 

其中dgCustDetails是DataGrid的名字。

+0

感谢您的帮助,这没有诀窍:) – neurotix 2012-04-02 06:09:17

1
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; 
相关问题