当鼠标悬停在窗口后,如何更改窗口的背景?

时间:2018-06-13 19:46:06

标签: c# wpf xaml

我已经了解了如何通过MouseClick或鼠标悬停时更改Button Background。如何在鼠标悬停时更改Window的背景?我之前没见过这样的东西......也许有人可以帮助我? 这是我用来更改Button背景的代码。我不知道我需要改变什么来做我需要的。

  <Window.Resources>
    <Style x:Key="DelAll">
        <Setter Property="Button.Template">
            <Setter.Value>
                <ControlTemplate>
                    <Grid>
                        <Border Name="Head">
                            <Rectangle HorizontalAlignment="Stretch"  VerticalAlignment="Stretch"  Fill="Transparent" />
                        </Border>
                        <Label Name="NameLable" Content="Delete All" FontStyle="Italic" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="Red"></Label>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="true">
                            <Setter TargetName="Head" Property="Background" Value="#3500ACFF"/>
                            <Setter TargetName="Head" Property="BorderBrush" Value="#FF00ACFF"/>
                            <Setter TargetName="Head" Property="BorderThickness" Value="1,1,1,1"/>
                            <Setter TargetName="NameLable" Property="Foreground" Value="Blue"/>
                        </Trigger>
                        <Trigger Property="IsMouseDirectlyOver" Value="true">
                            <Setter TargetName="NameLable" Property="Foreground" Value="White"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="Del">
        <Setter Property="Button.Template">
            <Setter.Value>
                <ControlTemplate>
                    <Grid>
                        <Border Name="Head">
                            <Rectangle HorizontalAlignment="Stretch"  VerticalAlignment="Stretch"  Fill="Transparent" />
                        </Border>
                        <Label Name="NameLable" Content="Delete" FontStyle="Italic" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="Yellow"></Label>
                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsMouseOver" Value="true">
                            <Setter TargetName="Head" Property="Background" Value="#3500ACFF"/>
                            <Setter TargetName="Head" Property="BorderBrush" Value="#FF00ACFF"/>
                            <Setter TargetName="Head" Property="BorderThickness" Value="1,1,1,1"/>
                            <Setter TargetName="NameLable" Property="Foreground" Value="Green"/>
                        </Trigger>
                        <Trigger Property="IsMouseDirectlyOver" Value="true">
                            <Setter TargetName="NameLable" Property="Foreground" Value="White"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>
<Grid>
    <Button Style="{StaticResource DelAll}" HorizontalAlignment="Left"  Width="150" Height="50" Margin="95,49,0,0" VerticalAlignment="Top"/>
    <Button Style="{StaticResource Del}" HorizontalAlignment="Left"  Width="150" Height="50" Margin="95,200,0,0" VerticalAlignment="Top"/>
</Grid>

1 个答案:

答案 0 :(得分:2)

此代码应该为您完成,<Window.Style> ... </Window.Style>是相关部分!

<Window x:Class="ForStackoverflow.MainWindow"
    ...
    xmlns:local="clr-namespace:ForStackoverflow"
    mc:Ignorable="d"
    Title="MainWindow" Height="350" Width="525">
    <Window.Style>
        <Style TargetType="{x:Type Window}">
        <Setter Property="Background" Value="Blue" />
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="true">
                    <Setter Property="Background" Value="Red" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Style>

    <Grid>
         Content..
    </Grid>
</Window>

它与按钮基本相同而不是在Style内创建<Windows.Resources>,而是使用{{1}设置Style的{​​{1}} }。希望这有帮助!