0

我创建了自己的CustomSlider( UserControl)。请看下面的代码:

后面的代码:

    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. 它也很好用。问题是我无法通过在更改with后Position单击顶部来更改。这是动画代码:CustomSliderPositionDoubleAnimation

        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吗?请帮助解决这个问题。谢谢,

4

1 回答 1

0

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

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

FillBehavior.Stop 将停止动画并可能将属性恢复到动画前的状态。

于 2013-06-21T15:15:55.193 回答