xaml - Windows 8 按钮在鼠标悬停时消失

标签 xaml windows-8 windows-runtime microsoft-metro winrt-xaml

我有一个设置控件如下(我保证我没有真正调用我的按钮 button_1 和 button_2):

<UserControl
x:Class="App1.SettingsPage"
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"
mc:Ignorable="d"
d:DesignHeight="300"
d:DesignWidth="646">
<Border BorderBrush="#FF590151" BorderThickness="1">
    <Grid Background="White" VerticalAlignment="Stretch">
        <Grid.RowDefinitions>
            <RowDefinition Height="80"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid Background="Aqua" Grid.Row="0">
            <Grid Margin="40,20,17,13">
                <Grid.Transitions>
                    <TransitionCollection>
                        <EntranceThemeTransition FromHorizontalOffset="50" />
                    </TransitionCollection>
                </Grid.Transitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="50" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
                <Button Click="Button_Click_1" Margin="0,3,0,0" Grid.Column="0"
                    HorizontalAlignment="Left" Style="{StaticResource BackButtonStyle}"/>
                <TextBlock Margin="10,5,0,0" Grid.Column="1" FontFamily="Segoe UI"
                    FontWeight="SemiLight" FontSize="24.6667" Text="Settings" HorizontalAlignment="Left" />
                <Image Source="/Assets/icon.png" HorizontalAlignment="Right" Grid.Column="2" Margin="0,0,6,0" />
            </Grid>
        </Grid>
        <Grid Grid.Row="1" Margin="40,24,23,0" VerticalAlignment="Top">

            <Grid.Transitions>
                <TransitionCollection>
                    <EntranceThemeTransition FromHorizontalOffset="120" />
                </TransitionCollection>
            </Grid.Transitions>

            <Button x:Name="button2" Foreground="Black" BorderThickness="1"
                    Click="button2_Click">Reset Difficulty</Button>

        </Grid>
    </Grid>
</Border>
</UserControl>

它工作正常,但是当我将鼠标悬停在 button2 上时它消失了。这是为什么?

最佳答案

您需要自定义按钮模板的PointerOver 视觉状态并将自定义样式应用于按钮。

查看 button styles and templates

默认的是

<VisualState x:Name="PointerOver">
    <Storyboard>
        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Border" Storyboard.TargetProperty="Background">
            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ButtonPointerOverBackgroundThemeBrush}" />
        </ObjectAnimationUsingKeyFrames>
        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ButtonPointerOverForegroundThemeBrush}" />
        </ObjectAnimationUsingKeyFrames>
    </Storyboard>
</VisualState>

要消除影响,只需将其注释掉即可。

<VisualState x:Name="PointerOver">
    <Storyboard>
        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Border" Storyboard.TargetProperty="Background">
            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ButtonPointerOverBackgroundThemeBrush}" />
        </ObjectAnimationUsingKeyFrames>
        <!--<ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource ButtonPointerOverForegroundThemeBrush}" />
        </ObjectAnimationUsingKeyFrames>-->
    </Storyboard>
</VisualState>

还有一种方法是更改​​ ButtonPointerOverForegroundThemeBrush 资源,但它会影响整个项目。

<SolidColorBrush x:Key="ButtonPointerOverForegroundThemeBrush" Color="Black" />

关于xaml - Windows 8 按钮在鼠标悬停时消失,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19248924/

相关文章:

c++ - 如何在发布版本中找到导致调试运行时依赖的原因

wpf - 引用包含合并字典的资源字典时出现问题

c# - 在代码隐藏中修改列表框的数据模板属性

c# - 在 Windows 8 Pro 平板电脑中使用 WinAPI 抛出 AccessViolationException

c# - 使用 C#/XAML 在 Metro 风格应用程序中将 Canvas 保存为图像

c# - Windows 8 中 WebView 的滚动问题

c# - .NET Core (coreclr) 中是否有 BitmapImage 的抽象?

wpf - WPF中的数据模板中的x:Key,x:Name和x:UID有什么区别?

encryption - 如何在没有 EFS 证书的情况下解密文件 (Windows 8)

javascript - Windows 8 应用程序中的背景音频