我希望在特定时间后关闭窗口,但这取决于属性。如果这是真的,它应该在6秒后关闭,否则在3秒后关闭。我想在xaml中完成几乎所有的事情。我对c#代码不满意。
这是我到目前为止所尝试的内容:
我的xaml:
<Window x:Class="embedUserc.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
xmlns:embedUserc="clr-namespace:embedUserc">
<Window.Triggers>
<DataTrigger Binding="{Binding IsSomething}" Value="False" >
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard Duration="0:0:6" FillBehavior="Stop" >
<BooleanAnimationUsingKeyFrames Storyboard.TargetName="CloseBehavior" Storyboard.TargetProperty="Close">
<DiscreteBooleanKeyFrame KeyTime="0:0:5" Value="True" />
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
<DataTrigger Binding="{Binding IsSomething}" Value="True" >
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard Duration="0:0:4" FillBehavior="Stop" >
<BooleanAnimationUsingKeyFrames Storyboard.TargetName="CloseBehavior" Storyboard.TargetProperty="Close" >
<DiscreteBooleanKeyFrame KeyTime="0:0:3" Value="True" />
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</Window.Triggers>
<Grid>
<embedUserc:WindowCloseBehavior x:Name="CloseBehavior"/>
</Grid>
我的c#代码:
using System.Windows;
namespace embedUserc
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new ViewModel();
}
}
public class WindowCloseBehavior : FrameworkElement
{
public static void SetClose(DependencyObject target, bool value)
{
target.SetValue(CloseProperty, value);
}
public static readonly DependencyProperty CloseProperty =
DependencyProperty.RegisterAttached(
"Close",
typeof(bool),
typeof(WindowCloseBehavior),
new UIPropertyMetadata(false, OnClose));
private static void OnClose(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
if (e.NewValue is bool && ((bool)e.NewValue))
{
Window window = GetWindow(sender);
if (window != null)
window.Close();
}
}
private static Window GetWindow(DependencyObject sender)
{
Window window = null;
if (sender is Window)
window = (Window)sender;
return window ?? (Window.GetWindow(sender));
}
}
public class ViewModel
{
public bool IsSomething { get { return true; } }
}
}
答案 0 :(得分:0)
我现在知道了,这是带有相同c#代码的工作xaml:
<Window.Resources>
<Style x:Key="CloseStyle">
<Style.Triggers>
<DataTrigger Binding="{Binding IsSomething}" Value="False" >
<DataTrigger.EnterActions>
<BeginStoryboard >
<Storyboard Duration="0:0:6" FillBehavior="Stop" >
<BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="Close" >
<DiscreteBooleanKeyFrame KeyTime="0:0:5" Value="True" />
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
<DataTrigger Binding="{Binding IsSomething}" Value="True" >
<DataTrigger.EnterActions>
<BeginStoryboard>
<Storyboard Duration="0:0:4" FillBehavior="Stop" >
<BooleanAnimationUsingKeyFrames Storyboard.TargetProperty="Close" >
<DiscreteBooleanKeyFrame KeyTime="0:0:3" Value="True" />
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</BeginStoryboard>
</DataTrigger.EnterActions>
</DataTrigger>
</Style.Triggers>
</Style>