难以使用依赖属性

时间:2013-05-07 21:06:54

标签: c# wpf xaml dependency-properties

我创建了自己的CustomSliderUserControl)。请参阅以下代码:

代码背后:

    public partial class CustomSlider : UserControl, INotifyPropertyChanged
    {

        public CustomSlider()
        {
            InitializeComponent();
            this.Loaded += CustomSlider_Loaded;
        }

        public static readonly DependencyProperty PositionProperty;

        static CustomSlider()
        {
            PositionProperty = DependencyProperty.Register("Position", typeof(double), typeof(CustomSlider), new FrameworkPropertyMetadata());
        }

        public double Position
        {
            get
            {
                return (double)GetValue(PositionProperty);
            }
            set
            {
                SetValue(PositionProperty, value);
                NotifyPropertyChanged("Position");
            }
        }

        double length = 0;

        void CustomSlider_Loaded(object sender, RoutedEventArgs e)
        {
            track.DataContext = this;
        }

        public double MaxValue { get; set; }

        public event EventHandler ValueChangedManually;

        //Changing the position manually
        private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            length = this.Width;
            if (Double.IsNaN(this.Width))
                length = this.ActualWidth;
            Point p = Mouse.GetPosition(this);
            try
            {
                //Position = p.X; // this didn't work too
                this.SetValue(PositionProperty, p.X);
            }
            catch (Exception ex)
            {
                //test
            }
        }

        #region INotifyPropertyChanged Members
        public event PropertyChangedEventHandler PropertyChanged;


        private void NotifyPropertyChanged(String info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
        #endregion
    }

XAML:

    <Grid>
        <Border BorderThickness="1" Background="White" BorderBrush="Blue" MouseLeftButtonDown="Border_MouseLeftButtonDown"/>
        <Label Name="track" Background="#FF4250D8" Width="{Binding Path=Position, Mode=TwoWay}" HorizontalAlignment="Left" MouseLeftButtonDown="Border_MouseLeftButtonDown" />
    </Grid>

我在表单中创建了一个新控件实例,并尝试通过单击Position顶部来更改CustomSlider(我的控件的一个属性)。它改变了我所期望的位置。然后我尝试使用DoubleAnimation执行此操作。它也可以正常工作。问题是,在使用Position更改CustomSlider后点击Position顶部,我无法更改DoubleAnimation。这是动画代码:

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            DoubleAnimation anim = new DoubleAnimation();
            anim.From = 0;
            anim.To =350;
            anim.Duration = TimeSpan.FromMilliseconds(1000);
            slider.BeginAnimation(CustomSlider.PositionProperty, anim);
        }

我的DependecyProperty有问题吗?请帮助解决这个问题。 谢谢,

1 个答案:

答案 0 :(得分:0)

@sa_ddam213评论的副本。这解决了我的问题。

因为动画仍然在运行,请尝试将Animations FillBehaviour设置为Stop anim.FillBehavior = FillBehavior.Stop;这将停止TimeLine并允许您在动画运行后修改Dependancyproperty

FillBehavior.Stop将停止动画,并可能将属性还原为其预先动画状态。