2015-04-01 65 views
1

在我的C#/ WPF应用程序,我有一个Datagrid与几个DataGridTextColumn列使用文字换行和工具提示。DataGridTextColumn样式与包装和工具提示

我可以写这样的每一列(这工作正常):

<DataGridTextColumn Header="Name" Binding="{Binding Name}"> 
    <DataGridTextColumn.ElementStyle> 
     <Style> 
      <Setter Property="TextBlock.TextWrapping" Value="Wrap" /> 
     </Style> 
    </DataGridTextColumn.ElementStyle> 
    <DataGridTextColumn.CellStyle> 
     <Style TargetType="DataGridCell"> 
      <Setter Property="ToolTip" Value="Some tooltip text" /> 
     </Style> 
    </DataGridTextColumn.CellStyle> 
</DataGridTextColumn> 

但我想定义一个通用的风格,可以设置包装和工具提示文本两种,明知工具提示文本对于每一列将是不同的。目的是为了避免代码冗余并使其更加清晰。

到目前为止,这是我的风格:

<Window.Resources> 
    <Style x:Key="WrapStyle" TargetType="{x:Type DataGridCell}"> 
     <Style.Setters> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type DataGridCell}"> 
         <TextBox Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content.Text}" TextWrapping="Wrap"> 
          <TextBox.ToolTip> 
           <ToolTip> 
            <ToolTip.Content> 
             <TextBlock Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Tooltip}" /> 
            </ToolTip.Content> 
           </ToolTip> 
          </TextBox.ToolTip> 
         </TextBox> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style.Setters> 
    </Style> 
</Window.Resources> 

而且我的专栏:

<DataGridTextColumn Header="Name" Binding="{Binding Name}" CellStyle="{StaticResource WrapStyle}" /> 

的问题是,我不能指定一个提示传递到风格。有没有办法做到这一点,而不用为每列写出5行DataGridTextColumn.CellStyle? 感谢

回答

1

修改样式 -

<Window.Resources> 
    <Style x:Key="WrapStyle" TargetType="DataGridCell"> 
     <Style.Setters> 
      <Setter Property="Template"> 
       <Setter.Value> 
        <ControlTemplate TargetType="{x:Type DataGridCell}"> 
         <TextBox Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content.Text}" TextWrapping="Wrap"> 
          <TextBox.ToolTip> 
           <ToolTip> 
            <ToolTip.Content> 
             <TextBlock Text="{Binding Path=Tooltip}"></TextBlock> 
            </ToolTip.Content> 
           </ToolTip> 
          </TextBox.ToolTip> 
         </TextBox> 
        </ControlTemplate> 
       </Setter.Value> 
      </Setter> 
     </Style.Setters> 
    </Style> 
</Window.Resources> 
+0

如何传递的工具提示文本从DataGridTextColumn风格?它不接受工具提示参数 – Noxxys 2015-04-01 12:00:58

+0

工具提示可以是当前绑定的项目的属性。即。如果将Student的集合绑定为项目源,并且Student类具有Tooltip属性,则可以将其设置为工具提示,如上所示。 – piyush 2015-04-01 13:22:38

+0

@piyush你能告诉我如何在** DataGridTextColumn **中使用上面的样式。你在之前的评论中解释过它,但我没有理解它。下面是它将如何定义,我试图了解如何将工具提示文本传递给这种风格? S52 2016-11-15 18:42:43