2012-03-30 66 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; 
相關問題