반응형
wpf 링크 버튼
하이퍼링크를 사용하지 않고 LinkButton처럼 보이게 하려면 어떻게 해야 하나요?
제안 사항
일반적인 버튼 스타일을 원하지 않고 하이퍼링크처럼 보이는 것을 원하는 경우 다음과 같이 시작할 수 있습니다.
<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" />
<Style x:Key="LinkButton"
TargetType="Button"
BasedOn="{StaticResource ResourceKey={x:Type Button}}"
>
<Setter Property="Width" Value="Auto"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="Button">
<ContentPresenter Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
VerticalAlignment="Center"
>
<ContentPresenter.Resources>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="TextDecorations" Value="Underline" />
</Style>
</ContentPresenter.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>
MichaC와 Anderson의 버전은 밑줄을 약간 잘못 긋습니다.다음은 모든 버전에 밑줄을 추가하는 업데이트 버전입니다.TextBlock그 안에 있는 것ContentPresenter.
다음은 MichaC의 제안입니다.Style모든 버튼에서 재사용할 수 있습니다.
<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>
가장 쉬운 방법(어플리케이션에서 이 작업을 수행합니다)
<TextBlock Name="..."
Text="..."
Cursor="Hand"
Foreground="Blue"
TextDecorations="Underline"
MouseLeftButtonUp=..."
/>
TextDecoration을 완전히 제어할 수 있습니다(예를 들어 펜 스타일이나 오프셋 변경).자세한 내용은 다음 링크를 참조하십시오.http://msdn.microsoft.com/en-us/library/system.windows.textdecorations.underline.aspx
다른 솔루션:Hyperlink안에 넣는 것이다.TextBlock.
<TextBlock>
<Hyperlink Click="...">
<TextBlock Text="Link text" />
</Hyperlink>
</TextBlock>
하이퍼링크를 사용하지 않으려는 이유는 무엇입니까?
<Button>
<Hyperlink>
</Button>
제안된 모든 솔루션의 조합:
승인된 버전과 같이 하드코드된 값이 없는 완성된 스타일입니다.
<Style
x:Key="HyperlinkButton"
TargetType="{x:Type Button}"
BasedOn="{StaticResource {x:Type Button}}"
>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Button}">
<TextBlock>
<Hyperlink
Command="{TemplateBinding Command}"
CommandTarget="{TemplateBinding CommandTarget}"
CommandParameter="{TemplateBinding CommandParameter}"
>
<ContentPresenter />
</Hyperlink>
</TextBlock>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
언급URL : https://stackoverflow.com/questions/780426/link-button-in-wpf
반응형
'programing' 카테고리의 다른 글
| xls-csv 변환기 (0) | 2023.04.10 |
|---|---|
| Windows 배치파일은 자신의 파일명을 결정할 수 있습니까? (0) | 2023.04.10 |
| 다른 지점에서 하나의 파일만 가져오려면 어떻게 해야 합니까? (0) | 2023.04.10 |
| 사전에서 키 목록을 가져오려면 어떻게 해야 합니까? (0) | 2023.04.10 |
| 문자열을 연결하는 가장 효율적인 방법? (0) | 2023.04.10 |