https://youtu.be/MGOb9QXi6So?list=PLxU-iZCqT52Cmj47aKB1T-SxI33YL7rYS

 

1) xaml만 수정해서 TextBlock 위에 마우스가 지나가면 글씨 속성 변경하기

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <TextBlock x:Name="tblk1" Text="Hello, WPF World!" FontSize="30"
                   HorizontalAlignment="Center" VerticalAlignment="Center">
            <TextBlock.Style>
                <Style TargetType="TextBlock">
                    <Setter Property="Foreground" Value="Green"></Setter>
                    <Style.Triggers>
                        <!-- 프로퍼티 트리거 -->
                        <Trigger Property="IsMouseOver" Value="true">
                            <Setter Property="Foreground" Value="Red"/>
                            <Setter Property="TextDecorations" Value="Underline" />
                        </Trigger>
                    </Style.Triggers>
                </Style>                
            </TextBlock.Style>
        </TextBlock>
    </Grid>
</Window>

MouseOver 이벤트

 

 

2) 스타일을 만들어서 두개의 컨트롤에 적용하기

<Window x:Class="WpfApp1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <Style x:Key="MyStyle">
            <Setter Property="Control.Foreground" Value="Red" />
            <Setter Property="TextBlock.Text" Value="Hello WPF!!"/>
            <Style.Triggers>
                <Trigger Property="Control.IsMouseOver" Value="true">
                    <Setter Property="Control.Foreground" Value="Blue" />
                    <Setter Property="TextBlock.Text" Value="버튼으로 진입했습니다"/>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <StackPanel>
        <Button Width="100" Height="70"
                Style="{StaticResource MyStyle}"
                Content="Trigger"
                />
        <TextBlock Style="{StaticResource MyStyle}"
                   FontSize="30" HorizontalAlignment="Center"
                VerticalAlignment="Center"/>
    </StackPanel>
</Window>

평상시
버튼 진입시
TextBlock 진입시

 

 

+ Recent posts