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

发布于 2024-11-29 07:31:25 字数 1414 浏览 2 评论 0原文

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

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

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

This seems as if it should be simple, but I can't make it happen... I've got a datagrid with a template column that includes a button:

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

and my linkbutton style looks like this:

<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. Is there an easy way to accomplish this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风渺 2024-12-06 07:31:25
<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 而不是 按钮

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

Or something like that...

(By the way, what about using the DataGridHyperlinkColumn, or at the very least just a normal Hyperlink instead of a Button?)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文