silverlightWPF 自定义VisualState 状态切换

silverlightWPF 自定义VisualState 状态切换,第1张

概述想要一个控件在选中和未选中时表现出两种状态,然后就想到了ToggleButton的Checked状态。但是多余的状态不想要,于是找度娘......顺便学习一下自定义控件的状态。 1、首先引用:Microsoft.Expression.Interactions.dll  2、创建一个控件,前台xmal中定义好自定义的状态改变时的动画。 StateControl.xaml, 用到了 ExtendedV

想要一个控件在选中和未选中时表现出两种状态,然后就想到了Togglebutton的Checked状态。但是多余的状态不想要,于是找度娘......顺便学习一下自定义控件的状态。


1、首先引用:Microsoft.Expression.Interactions.dll 

2、创建一个控件,前台xmal中定义好自定义的状态改变时的动画。

StateControl.xaml,用到了 Extendedvisualstatemanager,添加了Checked和Uncheck两个状态。

<UserControl x:Class="SL_StateTest.Controls.StateControl"    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:ei="http://schemas.microsoft.com/Expression/2010/interactions"    mc:Ignorable="d">    <GrID Background="CadetBlue"  x:name="LayoutRoot" WIDth="400" Height="400">        <visualstatemanager.Customvisualstatemanager>            <ei:Extendedvisualstatemanager/>        </visualstatemanager.Customvisualstatemanager>        <visualstatemanager.VisualStateGroups>            <VisualStateGroup x:name="CheckStates">                <VisualState x:name="Checked">                    <Storyboard>                        <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Visibility)" Storyboard.Targetname="CheckState">                            <discreteObjectKeyFrame KeyTime="0">                                <discreteObjectKeyFrame.Value>                                    <Visibility>Visible</Visibility>                                </discreteObjectKeyFrame.Value>                            </discreteObjectKeyFrame>                        </ObjectAnimationUsingKeyFrames>                    </Storyboard>                </VisualState>                <VisualState x:name="Unchecked"/>            </VisualStateGroup>        </visualstatemanager.VisualStateGroups>        <GrID x:name="CheckState" Visibility="Collapsed">            <border Background="brown"/>            <TextBlock Text="Checked" FontSize="50" Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Center"/>        </GrID>    </GrID></UserControl>



3、后台调用 Extendedvisualstatemanager

using System.windows;using System.windows.input;using Microsoft.Expression.Interactivity.Core;namespace SL_StateTest.Controls{    public partial class StateControl    {        #region 属性        public static Readonly DependencyProperty IsCheckedProperty =        DependencyProperty.Register("IsChecked",typeof(bool),typeof(StateControl),new PropertyMetadata(false,OnIsCheckedChanged));        public bool IsChecked        {            get { return (bool)GetValue(IsCheckedProperty); }            set { SetValue(IsCheckedProperty,value); }        }        #endregion        public StateControl()        {            InitializeComponent();        }        #region 事件        private static voID OnIsCheckedChanged(DependencyObject obj,DependencyPropertyChangedEventArgs args)        {            var statebutton = obj as StateControl;            if (statebutton != null)                statebutton.OnIsCheckedChanged();        }        private voID OnIsCheckedChanged()        {            Extendedvisualstatemanager.GotoElementState(LayoutRoot,IsChecked ? "Checked" : "Unchecked",false);        }        protected overrIDe voID OnMouseleftbuttonUp(MousebuttonEventArgs args)        {            args.Handled = true;            IsChecked = !IsChecked;            base.OnMouseleftbuttonUp(args);        }        #endregion    }}

DEMO: http://download.csdn.net/detail/wushang923/7493935 总结

以上是内存溢出为你收集整理的silverlight/WPF 自定义VisualState 状态切换全部内容,希望文章能够帮你解决silverlight/WPF 自定义VisualState 状态切换所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1013610.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-22
下一篇2022-05-22

发表评论

登录后才能评论

评论列表(0条)

    保存