微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

silverlight/WPF 自定义VisualState 状态切换

想要一个控件在选中和未选中时表现出两种状态,然后就想到了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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐