Zac*_*son 34 windows wpf xaml styles wpf-controls
我想在WPF中使用像链接样式的按钮.Microsoft在其Windows对话框中执行此操作(看似不一致).
它们看起来像蓝色文字.并在鼠标光标悬停时更改颜色和下划线.

我搞定了.(感谢Christian,Anderson Imes和MichaC)但是,我必须把一个TextBlock放在我的按钮内.
如何在不需要Button内的TextBlock的情况下改进我的风格?
<Button Style="{StaticResource HyperlinkLikeButton}">
<TextBlock>Edit</TextBlock>
</Button>
Run Code Online (Sandbox Code Playgroud)
<Style x:Key="HyperlinkLikeButton" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<ContentPresenter />
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HotTrackBrushKey}}" />
<Setter Property="Cursor" Value="Hand" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
<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>
</Trigger>
</Style.Triggers>
</Style>
Run Code Online (Sandbox Code Playgroud)
Mat*_*ský 36
你知道有超链接类/标签吗?它看起来像一个超链接,也可以作为按钮(可以使用URI和/或命令和/或点击事件).
编辑:
用法示例:
<TextBlock>
<Hyperlink Command="{Binding SomeCommand, ElementName=window}" CommandParameter="{Binding}">Link
</Hyperlink>
</TextBlock>
Run Code Online (Sandbox Code Playgroud)
Ayb*_*ybe 14
我参加派对有点晚了但是......
IMO最简单,最干净的方法:
<Style x:Key="HyperLinkButtonStyle" TargetType="Button">
<Setter Property="Focusable" Value="False" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<TextBlock>
<Hyperlink>
<Run Text="{TemplateBinding Content}" />
</Hyperlink>
</TextBlock>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
Run Code Online (Sandbox Code Playgroud)
小智 7
使用以下样式或模板不需要使用TextBlock元素:
<ControlTemplate x:Key="HyperlinkLikeButtonTemplate" TargetType="{x:Type Button}">
<TextBlock x:Name="innerText" Foreground="{DynamicResource {x:Static SystemColors.HotTrackBrushKey}}" Cursor="Hand" >
<ContentPresenter />
</TextBlock>
<ControlTemplate.Triggers>
<Trigger Property="Button.IsMouseOver" Value="true">
<Setter TargetName="innerText" Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}" />
<Setter TargetName="innerText" Property="TextDecorations" Value="Underline" />
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
<Style x:Key="HyperlinkLikeButton" TargetType="{x:Type Button}">
<Setter Property="Template" Value="{StaticResource HyperlinkLikeButtonTemplate}" />
</Style>
Run Code Online (Sandbox Code Playgroud)
<Button Style="{StaticResource HyperlinkLikeButton}" Content="Edit" />
Run Code Online (Sandbox Code Playgroud)
要么
<Button Style="{StaticResource HyperlinkLikeButton}">
Edit
</Button>
Run Code Online (Sandbox Code Playgroud)
或者您可以直接使用模板
<Button Template="{StaticResource HyperlinkLikeButtonTemplate}" Content="Edit" />
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
36006 次 |
| 最近记录: |