WPF按钮鼠标悬停或悬停更改矩形背景

时间:2014-11-17 18:19:16

标签: c# wpf xaml mouseover

当光标在指定按钮上时,我想更改矩形的(或其他)背景/图像源或可见性。

1 个答案:

答案 0 :(得分:1)

当鼠标悬停在按钮上时,此代码会更改矩形背景。

<Window x:Class="MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="400" Width="525">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="200"></RowDefinition>
            <RowDefinition Height="200"></RowDefinition>
        </Grid.RowDefinitions>
        <Button Width="200" Height="100" Content="Hover over me..." x:Name="btn">           
        </Button>
        <Rectangle Height="200" Grid.Row="1" x:Name="Rect1">
            <Rectangle.Style>
                <Style TargetType="Rectangle">
                    <Style.Triggers>
                        <DataTrigger Binding="{Binding ElementName=btn, Path=IsMouseOver}" Value="True">
                            <Setter Property="Fill" Value="Blue" />
                        </DataTrigger>
                    </Style.Triggers>
                </Style>
            </Rectangle.Style>            
        </Rectangle>
    </Grid>    
</Window>