2011-08-12 128 views
1

这好像应该是简单的,但我不能让它发生......我有一个模板列,其中包括一个按钮一个DataGrid:按钮风格

<DataGridTemplateColumn.CellTemplate> 
    <DataTemplate> 
     <StackPanel Orientation="Horizontal"> 
      <Button Style="{StaticResource LinkButton}" Content="{Binding Path=...}"/> 
     </StackPanel> 
    </DataTemplate> 
</DataGridTemplateColumn.CellTemplate> 

和我的LinkBut​​ton的风格是这样的:

<Style x:Key="LinkButton" TargetType="Button"> 
    <Setter Property="Template"> 
     <Setter.Value> 
      <ControlTemplate TargetType="Button"> 
       <ControlTemplate.Resources> 
        <Style TargetType="{x:Type TextBlock}"> 
         <Setter Property="TextDecorations" Value="Underline" /> 
        </Style> 
       </ControlTemplate.Resources> 
       <ContentPresenter /> 
      </ControlTemplate> 
     </Setter.Value> 
    </Setter> 
    <Setter Property="Foreground" Value="Blue" /> 
    <Setter Property="Cursor" Value="Hand" /> 
    <Style.Triggers> 
     <Trigger Property="IsMouseOver" Value="true"> 
      <Setter Property="Foreground" Value="Red" /> 
     </Trigger> 
    </Style.Triggers> 
</Style> 

当选择该行,我想LinkBut​​ton的的前景颜色更改为白色或东西。有没有简单的方法来完成这个?

回答

4
<Style.Triggers> 
    <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource AncestorType=DataGridRow}}" 
       Value="True"> 
      <Setter Property="TextElement.Foreground" Value="White"/> 
    </DataTrigger> 
    <!-- Here be your other trigger, order matters, if it is the other way around the above trigger overrides your mouse-over trigger --> 
</Style.Triggers> 

或者类似的东西...

顺便说一句,怎么样使用DataGridHyperlinkColumn,或者至少是只是一个普通的Hyperlink而不是Button)的

+0

神奇, 非常感谢。还有其他的东西在网格和列中强制我使用模板列。我在设计时尝试了超链接,但无法使其工作。我认为这是因为我需要发送一个命令给视图模型,在这一点上我不记得了。 – drowned