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

时间:2013-03-14 14:10:34

标签: xaml windows-8 windows-runtime winrt-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>

4 个答案:

答案 0 :(得分:0)

您无法使用DoubleAnimation控制可见性,因为可见性不是双精度。您应该使用ObjectAnimationUsingKeyFrames:

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

我还建议使用行为和视觉状态来使您的XAML更加设计友好。

答案 1 :(得分:0)

如果您只想翻转Visibility,可以使用绑定到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>

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

答案 2 :(得分:0)

我不确定你可以将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>

答案 3 :(得分:0)

WinRT XAML Toolkit

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