我正在处理一个项目,需要帮助寻找 Windows 8 XAML-only 解决方案,以便在将鼠标悬停在不同控件上时切换控件的可见性。控件可以是按钮或任何 Windows8 控件,代码需要在 XAML 中,因为我的所有逻辑都在 XAML 中。
我尝试了许多 XAML 解决方案,但我想我错过了一些东西。在我的第一次尝试中,我编写了一个事件触发器,但字符串无法转换为可见性,因此以下代码在执行时崩溃。
微软的任何人或任何专家都可以在这方面帮助我。我真的很感谢你的帮助。我正在寻找的是一个不需要任何代码隐藏的解决方案,它应该是完整的 XAML 代码。
<Page
x:Class="App1.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:App1"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<StackPanel Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
<Button Margin="5" x:Name="btn1">Button 1</Button>
<Button Margin="5" x:Name="btn2">
<Button.Triggers>
<EventTrigger RoutedEvent="Button.GotFocus">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="btn1" Storyboard.TargetProperty="Button.Visibility" To="Collapsed" Duration="0:0:1"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
<EventTrigger RoutedEvent="Button.LostFocus">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Storyboard.TargetName="btn1" Storyboard.TargetProperty="Button.Visibility" To="Visible" Duration="0:0:1"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Button.Triggers>
Button 2
</Button>
<Button Margin="5" x:Name="btn3">Button 3</Button>
</StackPanel>
</Page>
最佳答案
您不能使用 DoubleAnimation 控制 Visibility,因为 Visibility 不是 double 型。您应该使用 ObjectAnimationUsingKeyFrames:
<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="btn1">
<DiscreteObjectKeyFrame KeyTime="0">
<DiscreteObjectKeyFrame.Value>
<Visibility>Collapsed</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
我还建议使用行为和视觉状态使您的 XAML 对设计人员更友好。
关于xaml - 用于在将鼠标悬停在不同控件上时切换控件可见性的 Windows 8 XAML 解决方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15411634/