您的位置:首页 > 其它

WPF自动隐藏的消息框(鼠标放上去将一直显示,移开动画继续),提供normal和error两种边框。

2016-07-11 16:46 513 查看
原地址-> /article/11898562.html

介绍:传统的确定,取消,OK,CANCAL之类的对话框太繁琐了,由于项目需要而诞生的仿手机式提示对话框。当然传统的对话框项目中也有,这里就不做介绍了。

出场和退场动画做得很简单,就用Blend随便鼓捣了一番,将就用吧。

预览效果如下:

<Storyboard x:Key="ShowSb" Completed="Storyboard_Completed">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)" Storyboard.TargetName="grid1">
<EasingDoubleKeyFrame KeyTime="0" Value="{Binding YOffSet}"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:3" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:3.5" Value="{Binding YOffSet}"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)" Storyboard.TargetName="grid2">
<EasingDoubleKeyFrame KeyTime="0" Value="{Binding YOffSet}"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:3" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:3.5" Value="{Binding YOffSet}"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)" Storyboard.TargetName="tb">
<EasingDoubleKeyFrame KeyTime="0" Value="{Binding YOffSet}"/>
<EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:3" Value="0"/>
<EasingDoubleKeyFrame KeyTime="0:0:3.5" Value="{Binding YOffSet}"/>
</DoubleAnimationUsingKeyFrames>

</Storyboard>

<Storyboard x:Key="MouseLeave" Completed="Storyboard_Completed">
<DoubleAnimationUsingKeyFrames   Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="grid1">
<EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="0"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="grid2">
<EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="0"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="tb">
<EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="0"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)" Storyboard.TargetName="grid1">
<EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="{Binding YOffSet}"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)" Storyboard.TargetName="grid2">
<EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="{Binding YOffSet}"/>
</DoubleAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.RenderTransform).(TransformGroup.Children)[3].(TranslateTransform.Y)" Storyboard.TargetName="tb">
<EasingDoubleKeyFrame KeyTime="0:0:0.5" Value="{Binding YOffSet}"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>


View Code
注意:鼠标移动上去停止动画和移出后快速消失的代码为:

<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseEnter">
<ei:ControlStoryboardAction Storyboard="{StaticResource ShowSb}" ControlStoryboardOption="Stop"/>
</i:EventTrigger>
<i:EventTrigger EventName="MouseLeave">
<ei:ControlStoryboardAction Storyboard="{StaticResource MouseLeave}"/>
</i:EventTrigger>
</i:Interaction.Triggers>


后台代码逻辑:

private bool iserror = false;
public void Show(string messageBoxText, bool iserror = false)
{
this.iserror = iserror;
this.Message = messageBoxText;
this.Show();
}

public OnlyShowMessageBox()
{
InitializeComponent();
this.DataContext = new model() { YOffSet = -300d };
this.Loaded += (y, k) =>
{
this.Top = 41;
this.Left = (SystemParameters.WorkArea.Width) / 2 - this.ActualWidth / 2;
if (iserror)
{
this.grid1.Visibility = Visibility.Collapsed;
}
else { this.grid2.Visibility = Visibility.Collapsed; }
(this.Resources["ShowSb"] as Storyboard).Begin();
};
}

private void Storyboard_Completed(object sender, EventArgs e)
{
this.Close();
}


其中 :

public class model : ModelBase
{
private double YOffset;

public double YOffSet
{
get { return YOffset; }
set
{
YOffset = value;
RaisePropertyChangedEvent("YOffSet");
}
}

}


其中ModelBase在 可分组的选择框控件(MVVM下)(Toggle样式 仿造单选框RadioButton,复选框CheckBox功能) 中有介绍。

最后,调用方法:

new OnlyShowMessageBox().Show("请注意:前方高能,禁止入内!", false);

或者

new OnlyShowMessageBox().Show("不存在此对象!", true);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: