Application/C# (WPF)

[C# WPF] 마우스 올렸을때 변화 주기 (IsMouseOver Trigger)

devsalix 2023. 1. 12. 18:11
728x90

xaml 파일에서 마우스 올렸을 때  TextBlock의 글자 크기나 색상을

 

변경시키고 싶을 때 아래와 같이 작성하면 쉽게 구현이 가능합니다

 

<TextBlock Height="50" Width="150" Background="Gray" TextAlignment="Center" Text="마우스 오버" >
	<TextBlock.Style>
		<Style TargetType="TextBlock">
			<Setter Property="Padding" Value="0, 15" />
			<Setter Property="FontSize" Value="12" />
			<Setter Property="Foreground" Value="#D3D3D3" />
			<Style.Triggers>
				<Trigger Property="IsMouseOver" Value="true">
					<Setter Property="Padding" Value="0, 12" />
					<Setter Property="FontSize" Value="16" />
					<Setter Property="Foreground" Value="White" />
				</Trigger>
			</Style.Triggers>
		</Style>
	</TextBlock.Style>
</TextBlock>

 

위와 같이 작성을 하면

 

마우스를 올리기 전 기본일 때는 폰트 크기는 12 글자 색상은 #D3D3D3 값을 가지고 있다가

 

마우스가 올라오면 폰트 크기는 16으로 글자 색상은 흰색으로 변경됩니다

 

Padding의 경우 마우스 위치를 중앙에 맞춰주기 위해 임의의 값을 넣었습니다

 

 


 제 글이 도움이 되셨다면 댓글 & 공감 부탁드려요 😀

 
728x90