WPF DataGrid中选定行中的按钮样式

时间:2011-08-12 15:15:43

标签: wpf datagrid triggers styles

这似乎应该很简单,但我无法实现......我有一个带有包含按钮的模板列的数据网格:

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

我的linkbutton样式如下所示:

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

当选择行时,我想将链接按钮的前景色更改为白色或其他东西。有没有一种简单的方法可以实现这一目标?

1 个答案:

答案 0 :(得分: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