2009-11-14 56 views
10

我試過如下:無法設置的DataGridColumn的工具提示

<tk:DataGridTextColumn 
    Header="Item" 
    Binding="{Binding Item.Title}" 
    ToolTipService.ToolTip="{Binding Item.Description}" /> 

而且我沒有看到任何工具提示。

任何想法? 它甚至實施?

回答

24

這個工作對我來說:

<Style TargetType="{x:Type Custom:DataGridColumnHeader}"> 
    <Style.Triggers> 
     <Trigger Property="IsMouseOver" Value="True"> 
      <Setter Property="ToolTip" Value="{Binding Column.(ToolTipService.ToolTip), RelativeSource={RelativeSource Self}}"/> 
     </Trigger> 
    </Style.Triggers> 
</Style> 
+0

輝煌!這個小樣式現在使工具提示出現,不必將DataGridColumnHeaders更改爲DataGridTextColumns或任何東西。正是我在找什麼! – 2010-11-29 10:45:53

+0

嘿,你能告訴我如何將上述樣式添加到DataGridComboBoxColumn – Abhi 2012-05-08 08:51:22

7

請,請檢查下面的代碼會爲你工作,應該顯示的列標題和細胞提示,電池的提示應該彎曲的數據對象的描述字段:

<DataGridTextColumn Width="SizeToCells" 
        MinWidth="150" 
        Binding="{Binding Name}"> 

    <DataGridTextColumn.Header> 
     <TextBlock Text="Name" ToolTipService.ToolTip="Header ToolTip" /> 
    </DataGridTextColumn.Header> 

    <DataGridTextColumn.ElementStyle> 
     <Style TargetType="{x:Type TextBlock}"> 
      <Setter Property="ToolTip" Value="{Binding Description}" /> 
      <Setter Property="TextWrapping" Value="Wrap" /> 
     </Style> 
    </DataGridTextColumn.ElementStyle> 
</DataGridTextColumn> 

解決方案在這裏找到:5 Random Gotchas with the WPF DataGrid

4

DataGridTextColumn不可見。您必須在標題或內容上設置工具提示。

要設置在頭部中的工具提示,改變標頭給TextBlock:

<tk:DataGridTextColumn 
    Binding="{Binding Item.Title}"> 
    <tk:DataGridTextColumn.Header> 
    <TextBlock 
     Text="Text" 
     ToolTipService.ToolTip="Tooltip for header" /> 
    </tk:DataGridTextColumn.Header> 
</tk:DataGridTextColumn> 

要設置在列的內容的工具提示,將其設置在樣式:

<tk:DataGridTextColumn 
    Binding="{Binding Item.Title}" 
    Heading="Text"> 
    <tk:DataGridTextColumn.ElementStyle> 
    <Style> 
     <Setter Property="ToolTipService.ToolTip" Value="{Binding Item.Description}" /> 
    </Style> 
    </tk:DataGridTextColumn.ElementStyle> 
</tk:DataGridTextColumn> 

您可能也想設置EditingElementStyle

0

集ToolTipService.ToolTip屬性,標題樣式:

<Setter Property="ToolTipService.ToolTip" Value="{x:Static res:StringResources.List_Dialog_SelectAll_Checkbox}"/> 

這是我如何使用它,當我在DataGridCheckBoxColumn而不是文字有圖片。 XAML:

<Window x:Class="MyProject.GUI.ListDialog" 
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
      xmlns:viewModel="clr-MyProject.GUI.ViewModels" 
      Title="{Binding Title}" Height="350" Width="650" 
      MinHeight="350" MinWidth="650" 
      xmlns:res="clr-MyProject.GUI.Resources" Closing="Window_Closing" WindowStyle="ToolWindow"> 
    <Window.Resources> 
      <BitmapImage x:Key="MyImageSource" UriSource="Resources/Images/SelectDeselect.png" /> 
      <Style x:Key="CheckBoxHeader" TargetType="DataGridColumnHeader"> 
       <Setter Property="HorizontalContentAlignment" Value="Center"/> 
       <Setter Property="VerticalContentAlignment" Value="Center"/> 
          <Setter Property="ToolTipService.ToolTip" Value="{x:Static res:StringResources.List_Dialog_SelectAll_Checkbox}"/> 
       <Setter Property="ContentTemplate"> 
        <Setter.Value> 
         <DataTemplate> 
          <Image Width="15" Height="15" Source="{StaticResource MyImageSource}" /> 
         </DataTemplate> 
        </Setter.Value> 
       </Setter> 
      </Style> 
    </Window.Resources> 

C#:

DataGridCheckBoxColumn checkColumn = new DataGridCheckBoxColumn(); 
checkColumn.HeaderStyle = new System.Windows.Style(); 
checkColumn.CanUserSort = checkColumn.CanUserResize = false; 
checkColumn.Width = new DataGridLength(25); 
checkColumn.HeaderStyle = (Style)Resources["CheckBoxHeader"]; 
checkColumn.CellStyle = (Style)Resources["CenterAlignedCellStyle"]; 
checkColumn.IsReadOnly = false; 
dataGrid.Columns.Add(checkColumn); 
1

此外,如果你的列是DataGridTemplateColumn代替DataGridTextColumn,你可以做這樣的:

<DataGridTemplateColumn x:Name="MyCheckBoxColumn" CellStyle="{StaticResource MyCellStyle}" > 
    <DataGridTemplateColumn.HeaderTemplate> 
     <DataTemplate> 
      <TextBlock Text="MyHeaderName" ToolTip="This is my column description" /> 
     </DataTemplate> 
    </DataGridTemplateColumn.HeaderTemplate> 
    <DataGridTemplateColumn.CellTemplate> 
     <DataTemplate> 
      <CheckBox ... /> 
     </DataTemplate> 
    </DataGridTemplateColumn.CellTemplate> 
</DataGridTemplateColumn>