wpf中的链接按钮
如何使按钮看起来像LinkButton,而我不想使用 超链接wpf中的链接按钮,wpf,wpf-controls,Wpf,Wpf Controls,如何使按钮看起来像LinkButton,而我不想使用 超链接 任何建议为什么不想使用超链接 <Button> <Hyperlink> </Button> 如果你不想要任何普通的按钮样式,只想要看起来像超链接的东西,你可以从这个开始 <Button Margin="5" Content="Test" Cursor="Hand"> <Button.Template> <ControlTemplate
任何建议为什么不想使用超链接
<Button>
<Hyperlink>
</Button>
如果你不想要任何普通的按钮样式,只想要看起来像超链接的东西,你可以从这个开始
<Button Margin="5" Content="Test" Cursor="Hand">
<Button.Template>
<ControlTemplate TargetType="Button">
<TextBlock TextDecorations="Underline">
<ContentPresenter />
</TextBlock>
</ControlTemplate>
</Button.Template>
<Button.Style>
<Style TargetType="Button">
<Setter Property="Foreground" Value="Blue" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="true">
<Setter Property="Foreground" Value="Red" />
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
这与样式相同:
<Style
x:Key="LinkButton"
TargetType="Button">
<Setter
Property="Template">
<Setter.Value>
<ControlTemplate
TargetType="Button">
<TextBlock
TextDecorations="Underline">
<ContentPresenter /></TextBlock>
</ControlTemplate>
</Setter.Value>
</Setter>
<Setter
Property="Foreground"
Value="Blue" />
<Style.Triggers>
<Trigger
Property="IsMouseOver"
Value="true">
<Setter
Property="Foreground"
Value="Red" />
</Trigger>
</Style.Triggers>
</Style>
您可以这样使用它:
<Button Style="{StaticResource LinkButton}" Content="Clicky" />
以下是MichaC的建议,它以
风格实现,您可以在任何按钮上重复使用:
<Style x:Key="LinkButton" TargetType="Button">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<TextBlock TextDecorations="Underline">
<ContentPresenter />
</TextBlock>
</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>
MichaC和Anderson的版本将下划线放在了稍微错误的位置,下面是一个更新版本,它只会将下划线添加到ContentPresenter
中的任何TextBlock
,这是最简单的方法(我在应用程序中这样做):
使用超链接的另一个解决方案是将文本块
放入内部
<TextBlock>
<Hyperlink Click="...">
<TextBlock Text="Link text" />
</Hyperlink>
</TextBlock>
Coz,每当我试图在可视化树中获取父对象时,它都会抛出一个异常,说Hyperlink不是可视化的,或者Visual3dI想使用它。我让它工作,但我似乎无法摆脱按钮边缘的阴影。你是怎么做到的?考虑到99%的答案都是从MichaC偷来的,这是一个社区维基:)如果有人在意,这里所有答案的问题是,答案中的链接实际上不像链接。他们不知道访问的url历史记录,并且颜色不是系统url颜色。但是,如果你没有这些要求,它们是可以的。我正在试图找出如何使用正确的Windows颜色。克里斯蒂安:为什么我看不到任何使用你风格的下划线?米哈奇对我很好,这对我也不起作用。ContentPresenter.Resources中的样式不会应用于ButtonOK中的任何文本块,问题似乎在于,该样式仅适用于隐式生成的文本BlockText works不起作用请注意,此处有一个输入错误-LinkButton此答案生成一个按钮,该按钮在下划线和文本之间有一个空格@基督徒的回答解决了这个问题。当鼠标悬停在超链接上时,它也没有“手”指针。使用社区wiki回答此功能。我建议添加
来处理IsEnabled State错误做法,按照建议设置链接按钮的样式要好得多。。。如果你有50个这样的链接按钮呢?另外,如果你需要添加一个悬停动作或者一个不同的装饰,那该怎么办?我不同意托默的观点。“坏习惯”意味着永远不需要这种更简单的方法。我现在有一个案例,这是一个完美的解决方案,即在代码隐藏中创建按钮)。谢谢Siavash。是的,这不是一个坏习惯。对于某些场景来说,它将是完美的。
<TextBlock Name="..."
Text="..."
Cursor="Hand"
Foreground="Blue"
TextDecorations="Underline"
MouseLeftButtonUp=..."
/>
<TextBlock>
<Hyperlink Click="...">
<TextBlock Text="Link text" />
</Hyperlink>
</TextBlock>