2013-05-07 2 views
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을 사용하여이 작업을 시도했습니다. 그것은 잘 작동합니다. 문제 : PositionDoubleAnimation으로 변경 한 후 CustomSlider 상단을 클릭하여 Position을 변경할 수 없습니다. 다음은 애니메이션 코드입니다.

 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에 문제가 있습니까? 이 문제를 해결하는 데 도움을주십시오. 감사합니다,

+0

호기심에서 벗어나서 슬라이더가 기본 슬라이더에서 할 수없는 작업은 무엇입니까? –

+0

@ChrisW. 오디오를 재생하는 어플리케이션을 만들고 있습니다. 현재 재생중인 위치를 표시해야합니다. 사용자가 수동으로 변경할 수 있어야합니다. Default Slider는 똑같은 일을하지만 내 클라이언트는 다른 종류의 UI를 원합니다. – Dilshod

+3

[종속성 속성 값 우선 순위] (http://msdn.microsoft.com/en-us/library/ms743230.aspx)를 읽습니다. 애니메이션은 최우선 순위 중 하나입니다. 처리기에서 "로컬"로 간주되는 속성을 설정합니다. 애니메이션에서 두 개의 명령입니다. 따라서 속성을 설정하기 전에 애니메이션을 중지해야합니다. 또한 종속성 속성에서 변경 알림을 필요로하지 않습니다. 마지막으로, UI를 변경하려면 자신의 롤링 대신 'ControlTemplate'을 사용하십시오. – XAMeLi

답변

0

@sa_ddam213 님의 댓글의 복사본. 그게 내 문제를 해결해 줬어.

애니메이션이 여전히 실행 중이기 때문에 Animations FillBehaviour를 Stop으로 설정해보십시오. anim.FillBehavior = FillBehavior.Stop; 이렇게하면 TimeLine이 중지되고 애니메이션 실행 후 Dependancyproperty를 수정할 수 있습니다.

FillBehavior.Stop은 애니메이션을 중지하고 속성을 애니메이션 전 상태로 되돌릴 수 있습니다.

관련 문제