2
由於其他原因,我需要WPF CustomControl。我的目標是幻燈片窗口,有財產IsActive。CustomControl滑動窗口
如果此屬性更改爲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>
SlideWindowControl在 使用的
例MainWindow.xaml
<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設置visibility屬性也觸發了SlideWindowControl(見註釋代碼請),但它會中斷整個觸發器(不顯示動畫效果)。
如何才能實現可見性屬性的更改?我需要在其他組件中綁定到可見性屬性。