【问题标题】:Button style in selected row in WPF DataGridWPF DataGrid中选定行中的按钮样式
【发布时间】:2011-08-12 15:15:43
【问题描述】:

这似乎应该很简单,但我做不到......我有一个带有模板列的数据网格,其中包含一个按钮:

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

我的链接按钮样式如下所示:

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

When the row is selected, I want to change the foreground color of the linkbutton to white or something.有没有简单的方法可以做到这一点?

【问题讨论】:

    标签: wpf datagrid triggers styles


    【解决方案1】:
    <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

    【讨论】:

    • 太棒了,非常感谢。网格和列中还有其他内容迫使我使用模板列。我在设计时尝试了超链接,但无法让它工作。我想是因为我需要向视图模型发送命令,我现在不记得了。
    猜你喜欢
    • 2011-05-31
    • 2013-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多