用于将鼠标悬停在其他控件上时切换控件可见性的Windows 8 XAML唯一解决方案

用于将鼠标悬停在其他控件上时切换控件可见性的Windows 8 XAML唯一解决方案,xaml,windows-8,windows-runtime,winrt-xaml,Xaml,Windows 8,Windows Runtime,Winrt Xaml,我正在从事一个项目,需要帮助找到一个仅适用于Windows 8 XAML的解决方案,以便在将鼠标悬停在其他控件上时切换控件的可见性。该控件可以是按钮或任何Windows8控件,并且代码需要使用XAML,因为我的所有逻辑都使用XAML。 我尝试了许多XAML解决方案,但我想我遗漏了一些东西。在我的第一次尝试中,我编写了一个事件触发器,但无法将字符串转换为可见性,因此以下代码在执行时崩溃 微软的任何人或任何专家都能在这方面帮助我吗。我真的很感谢你的帮助。我正在寻找的是一个解决方案,不需要任何代码背后

我正在从事一个项目,需要帮助找到一个仅适用于Windows 8 XAML的解决方案,以便在将鼠标悬停在其他控件上时切换控件的可见性。该控件可以是按钮或任何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>

按钮1
按钮2
按钮3

您不能使用double动画控制可见性,因为可见性不是double动画。您应该使用ObjectAnimationUsingKeyFrames:

<ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.TargetName="btn1">
    <DiscreteObjectKeyFrame KeyTime="0">
        <DiscreteObjectKeyFrame.Value>
            <Visibility>Collapsed</Visibility>
        </DiscreteObjectKeyFrame.Value>
    </DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>

崩溃

我还建议使用行为和可视状态使XAML对设计师更友好。

如果您只想翻转
可见性
,您可以使用绑定到
IsPointerOver
属性:

<StackPanel Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
    <StackPanel.Resources>
        <local:BooleanToVisibilityConverter x:Key="BooleanToVisibilityConverter"/>
    </StackPanel.Resources>
    <Button Margin="5" x:Name="btn1">Button 1</Button>

    <Button Margin="5" x:Name="btn2"  Visibility="{Binding ElementName=btn1, Path=IsPointerOver, Converter={StaticResource BooleanToVisibilityConverter}}">
        Button 2
    </Button>
</StackPanel>

按钮1
按钮2

您需要为
BooleantVisibilityConverter添加代码,因为它不像WPF中那样内置。

我不确定您是否可以将EventTrigger与RoutedEvent一起使用。它可能会有用吗

 <Page.Resources>
    <Style x:Key="TagAppBarButtonStyle" TargetType="Button" BasedOn="{StaticResource AppBarButtonStyle}">
      <Setter Property="Template">
        <Setter.Value>
          <ControlTemplate TargetType="Button">
            <Border x:Name="AppButton" Width="{TemplateBinding Width}" Height="{TemplateBinding Height}">
              <VisualStateManager.VisualStateGroups>
                <VisualStateGroup x:Name="CommonStates">
                  <VisualState x:Name="Normal">
                    <Storyboard>
                      <Storyboard>
                        <DoubleAnimation Duration="0" To="0" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="tbName" />
                      </Storyboard>
                    </Storyboard>
                  </VisualState>
                  <VisualState x:Name="PointerOver">
                    <Storyboard>
                      <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="tbName" />
                    </Storyboard>
                  </VisualState>

                </VisualStateGroup>



              </VisualStateManager.VisualStateGroups>
              <Grid Width="100">
                <Rectangle Width="{TemplateBinding Width}" Height="{TemplateBinding Height}" Fill="Transparent"/>
                <TextBlock Text="Button 2" x:Name="tbName" />

              </Grid>
            </Border>
          </ControlTemplate>
        </Setter.Value>
      </Setter>
    </Style>
  </Page.Resources>
  <Grid>
  <StackPanel Background="{StaticResource ApplicationPageBackgroundThemeBrush}">
    <Button Margin="5" x:Name="btn1" Content="Button 1" />
    <Button Margin="5" x:Name="btn2" Style="{StaticResource TagAppBarButtonStyle}" />
    <Button Margin="5" x:Name="btn3" Content="Button 3" />

  </StackPanel>
  </Grid>


框架中有许多内置转换器,包括BooleantVisibilityConverter。

Denis,谢谢。。由于我不确定将您共享的代码片段放在何处,您可以编写整个XAML吗?只需将代码替换为“我的代码”,并将可见性更新为“LostFocus触发器可见”。我的代码无法编译,因为WinRT XAML中没有事件触发器。我只是把它拿出来展示逻辑,代码不起作用。所以我只是问你是否可以写整个XAML。这不是WPF,而是Windows8 XAML