wpf 마우스오버 색변경

2024. 8. 23. 10:16젬스it

반응형

WPF에서 Grid로 감싸진 Border와 TextBlock에서 마우스 오버 시 Border 색과 TextBlock 텍스트 색을 동시에 변경하는 방법
스타일 trigger 전에 기본색값셋팅 먼저체크할것!!

<Window x:Class="YourNamespace.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MouseOver Border and TextBlock Example" Height="300" Width="400">
    <Grid>
        <Border BorderBrush="White" BorderThickness="1">
            <Border.Style>
                <Style TargetType="{x:Type Border}">
                    <Setter Property="BorderBrush" Value="White" />
                    <Style.Triggers>
                        <Trigger Property="IsMouseOver" Value="True">
                            <Setter Property="BorderBrush" Value="Yellow" />
                        </Trigger>
                    </Style.Triggers>
                </Style>
            </Border.Style>
            <TextBlock Text="Hover over me!" FontSize="16">
                <TextBlock.Style>
                    <Style TargetType="{x:Type TextBlock}">
                        <Setter Property="Foreground" Value="White" />
                        <Style.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                                <Setter Property="Foreground" Value="Yellow" />
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </TextBlock.Style>
            </TextBlock>
        </Border>
    </Grid>
</Window>

반응형