CustomControl幻灯片窗口

时间:2016-08-01 12:38:46

标签: c# wpf xaml custom-controls

由于其他原因,我需要 WPF CustomControl 。我的目标是幻灯片窗口,其属性 IsActive

如果此属性更改为true,则幻灯片窗口从左向右滑动。如果此属性从true更改回来,则SlideWindow会滑出。

此功能正常(我在 XAML 文件中通过触发器执行此操作)。我还需要将可见性设置为 Visibile (在幻灯片开始生效之前)或折叠(滑出效果完成后)。这就是问题所在。

SlideWindowControl.cs

using System;
using System.Windows;
using System.Windows.Controls;

namespace SlideWindowTest
{
    public class SlideWindowControl : ContentControl
    {
        public static readonly DependencyProperty IsActiveProperty = DependencyProperty.Register(
          "IsActive", typeof(Boolean), typeof(SlideWindowControl), new PropertyMetadata(true));

        static SlideWindowControl()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(SlideWindowControl), new FrameworkPropertyMetadata(typeof(SlideWindowControl)));
        }

        public Boolean IsActive
        {
            get { return (Boolean)GetValue(IsActiveProperty); }
            set { SetValue(IsActiveProperty, value); }
        }
    }
}

对应 Generic.xaml SlideWindowControl

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:SlideWindowTest">

    <Style TargetType="{x:Type local:SlideWindowControl}">
        <Setter Property="RenderTransform">
            <Setter.Value>
                <ScaleTransform ScaleX="1"/>
            </Setter.Value>
        </Setter>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type local:SlideWindowControl}">
                    <Border Background="{TemplateBinding Background}"
                            BorderBrush="{TemplateBinding BorderBrush}"
                            BorderThickness="{TemplateBinding BorderThickness}"
                            x:Name="Root">
                        <StackPanel Orientation="Vertical">
                            <Grid Background="Black">
                                <TextBlock Text="HEADER" Foreground="White" Margin="17" FontSize="20pt" />
                            </Grid>
                            <ContentPresenter />
                        </StackPanel>
                    </Border>

                    <ControlTemplate.Triggers>
                        <Trigger Property="IsActive" Value="True">

                            <!-- Show -->
                            <Trigger.EnterActions>
                                <BeginStoryboard>
                                    <Storyboard>
                                        <!-- Visibility change cause trouble
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Duration="0" BeginTime="0">
                                            <DiscreteObjectKeyFrame Value="{x:Static Visibility.Visible}" />
                                        </ObjectAnimationUsingKeyFrames>
                                        -->

                                        <DoubleAnimation Storyboard.TargetProperty="RenderTransform.ScaleX" From="0" Duration="0:00:00.5" />
                                    </Storyboard>
                                </BeginStoryboard>
                            </Trigger.EnterActions>

                            <!-- Hide -->
                            <Trigger.ExitActions>
                                <BeginStoryboard>
                                    <Storyboard>
                                        <DoubleAnimation Storyboard.TargetProperty="RenderTransform.ScaleX" From="1" To="0" Duration="0:00:00.5"/>

                                        <!-- Visibility change cause trouble
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility" Duration="0" BeginTime="0:00:00.5">
                                            <DiscreteObjectKeyFrame Value="{x:Static Visibility.Collapsed}" />
                                        </ObjectAnimationUsingKeyFrames>
                                        -->
                                    </Storyboard>
                                </BeginStoryboard>
                            </Trigger.ExitActions>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</ResourceDictionary>

MainWindow.xaml

中使用 SlideWindowControl 的示例
<Window x:Class="SlideWindowTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:SlideWindowTest"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel Orientation="Vertical">
        <StackPanel Orientation="Horizontal">
            <Button Content="Slide In" Click="SlideInButton_Click"/> <!-- InMainWindow.xaml.cs set MySlideWindow.IsActive = true; -->
            <Button Content="Slide Out" Click="SlideOutButton_Click" /> <!-- InMainWindow.xaml.cs set MySlideWindow.IsActive = false; -->
        </StackPanel>

        <local:SlideWindowControl x:Name="MySlideWindow">
            <TextBlock FontSize="40pt">Super Content!</TextBlock>
        </local:SlideWindowControl>
    </StackPanel>
</Window>

我尝试在 Generic.xaml 的触发器中为 SlideWindowControl 设置可见性属性(请参阅注释代码),但它会打破整个触发(不显示动画效果)。

如何更改可见性属性?我需要绑定到其他组件中的可见性属性

1 个答案:

答案 0 :(得分:1)

解决方案可能是:

  1. 将两个故事板创建为静态资源,并在BeginStoryboard-Storyboard({StaticResource SlideIn})中引用它们,而不是将它们作为内联子项。

  2. 实施已完成的活动

  3. 在完成事件处理程序中,当IsActive = false时,您将可见性设置为折叠。
  4. 在FadeIn-click事件中,您必须在设置IsActive = true之前将Visibility设置为Visible。
  5. 这有点不对称,但它适用于我的测试用例。

        <Window x:Class="SO38699140.MainWindow"
            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"
            xmlns:local="clr-namespace:SO38699140"
            mc:Ignorable="d"
            Title="MainWindow" Height="350" Width="525">
      <Window.Resources>
    
        <Storyboard x:Key="SlideIn" Completed="Storyboard_Completed" >
          <DoubleAnimation Storyboard.TargetProperty="RenderTransform.ScaleX" From="0" Duration="0:00:00.5"  />
        </Storyboard>
    
        <Storyboard x:Key="SlideOut" Completed="Storyboard_Completed">
          <DoubleAnimation Storyboard.TargetProperty="RenderTransform.ScaleX" From="1" To="0" Duration="0:00:00.5" />
        </Storyboard>
    
    
        <Style TargetType="{x:Type local:SlideWindowControl}">
          <Setter Property="RenderTransform">
            <Setter.Value>
              <ScaleTransform ScaleX="1"/>
            </Setter.Value>
          </Setter>
          <Setter Property="Template">
            <Setter.Value>
              <ControlTemplate TargetType="{x:Type local:SlideWindowControl}">
                <Border Background="{TemplateBinding Background}"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                x:Name="Root">
                  <StackPanel Orientation="Vertical">
                    <Grid Background="Black">
                      <TextBlock Text="HEADER" Foreground="White" Margin="17" FontSize="20pt" />
                    </Grid>
                    <ContentPresenter />
                  </StackPanel>
                </Border>
    
                <ControlTemplate.Triggers>
                  <Trigger Property="IsActive" Value="True">
    
                    <!-- Show -->
                    <Trigger.EnterActions>
                      <BeginStoryboard Storyboard="{StaticResource SlideIn}">
                      </BeginStoryboard>
                    </Trigger.EnterActions>
    
                    <!-- Hide -->
                    <Trigger.ExitActions>
                      <BeginStoryboard Storyboard="{StaticResource SlideOut}">
                      </BeginStoryboard>
                    </Trigger.ExitActions>
                  </Trigger>
                </ControlTemplate.Triggers>
              </ControlTemplate>
            </Setter.Value>
          </Setter>
        </Style>
      </Window.Resources>
        <StackPanel Orientation="Vertical">
          <StackPanel Orientation="Horizontal">
            <Button Content="Slide In" Click="SlideInButton_Click"/>
            <!-- InMainWindow.xaml.cs set MySlideWindow.IsActive = true; -->
            <Button Content="Slide Out" Click="SlideOutButton_Click" />
            <!-- InMainWindow.xaml.cs set MySlideWindow.IsActive = false; -->
          <TextBlock Text="{Binding ElementName=MySlideWindow, Path=Visibility}" />
    
        </StackPanel>
    
          <local:SlideWindowControl x:Name="MySlideWindow">
            <TextBlock FontSize="40pt">Super Content!</TextBlock>
          </local:SlideWindowControl>
      </StackPanel>
    </Window>
    
            }
    
        private void SlideInButton_Click(object sender, RoutedEventArgs e)
        {
          MySlideWindow.Visibility = Visibility.Visible;
          MySlideWindow.IsActive = true;
        }
    
        private void SlideOutButton_Click(object sender, RoutedEventArgs e)
        {
          MySlideWindow.IsActive = false;
        }
    
        private void Storyboard_Completed(object sender, EventArgs e)
        {
          if (!MySlideWindow.IsActive)
            MySlideWindow.Visibility = Visibility.Collapsed;
    
        }