我创建了自己的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
单击顶部来更改。这是动画代码: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
吗?请帮助解决这个问题。谢谢,