2010-07-13 4 views
0

동적 경로의 끝점을 얻고 그 위에 객체를 추가하는 방법을 찾고 있습니다 -이 종류의 패턴과 유사합니다 :WPF/C# : 동적 경로의 끝점을 가져 와서 객체를 추가하십시오.

alt text http://i29.tinypic.com/159182.png

여기서 빨간색 원은 주어진 경로의 끝 점이있는 곳입니다. 대신에, 경로가 이렇게 만들어 있다는 사실을 :

<Path x:Name="path" Data="M621,508 L582.99987,518.00011 569.99976,550.00046 511.9996,533.00032 470.9995,509 485.99953,491.99981" Margin="469,0,0,168" Stretch="Fill" Stroke="Black" StrokeThickness="4" Height="62" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="154" Visibility="Hidden"/> 

나는이 사용했다 :

나는 데이터베이스에서 경로 데이터를 얻을 수
<Path Stroke="Black" x:Name="path1" Data="{Binding MyProperty1}" Margin="0" StrokeThickness="4"/> 

.

의견이나 제안이 있으십니까?

추신. 경로의 끝점에 객체/이미지 (움직이거나 움직이지 않는)를 배치하려고합니다.

+0

당신의 경로 databinds는 'MyProperty1'입니다. 아마 경로로 변환 할 수있는 몇 가지 바인드 가능한 데이터 유형입니다. 이 속성은 포인트 목록입니까? 그렇다면 collection \ path의 마지막 점에 대한 "getter"인 뷰 모델에 또 다른 바인드 가능 속성을 만듭니다. 이미지 또는 효과를이 새 속성에 데이터 바인딩합니다. –

답변

0

흠, 내 위의 의견을 되풀이, 아마도 당신은이

public class PathViewModel 
{ 
    public ObservableCollection<Point> Points { get; private set; } 
    PathViewModel() 
    { 
     Points = new ObservableCollection<Point>(); 
    } 
} 

과 같이 단순히 INotifyPropertyChanged을 구현 및 경로의 마지막 지점에 대한 명시 적 속성을 생성하여이 모델을 확장해야

public class PathViewModel : INotifyPropertyChanged 
{ 
    private static readonly PropertyChangedEventArgs OmegaPropertyChanged = 
     new PropertyChangedEventArgs ("Omega"); 

    // returns true if there is at least one point in list, false 
    // otherwise. useful for disambiguating against an empty list 
    // (for which Omega returns 0,0) and real path coordinate 
    public bool IsOmegaDefined { get { return Points.Count > 0; } } 

    // gets last point in path, or 0,0 if no points defined 
    public Point Omega 
    { 
     get 
     { 
      Point omega; 
      if (IsOmegaDefined) 
      { 
       omega = Points[Points.Count - 1]; 
      } 
      return omega; 
     } 
    } 

    // gets points in path 
    public ObservableCollection<Point> Points { get; private set; } 

    PathViewModel() 
    { 
     Points = new ObservableCollection<Point>(); 
    } 

    // interfaces 

    #region INotifyPropertyChanged Members 

    public event PropertyChangedEventHandler PropertyChanged; 

    #endregion 

    // private methods 

    private void Points_CollectionChanged (
     object sender, 
     NotifyCollectionChangedEventArgs e) 
    { 
     // if collection changed, chances are so did Omega! 
     if (PropertyChanged != null) 
     { 
      PropertyChanged (this, OmegaPropertyChanged); 
     } 
    } 

} 

주목할 가치가있는 유일한 것은 컬렉션이 변경 될 때 속성 변경 이벤트를 실행하는 것입니다. 이 모델이 변경되었음을 WPF에 알립니다.

자, XAML의 땅에서,

<!-- assumes control's DataContext is set to instance of PathViewModel --> 
<Path 
    Stroke="Black" 
    x:Name="path1" 
    Data="{Binding Path=Points}" 
    Margin="0" 
    StrokeThickness="4"/> 
<!-- or whatever control you like, button to demonstrate binding --> 
<Button 
    Content="{Binding Path=Omega}" 
    IsEnabled="{Binding Path=IsOmegaDefined}"/> 

좋아, 그래서 IsEnabled 위의 이미지 \ 버튼을 숨길 수 있지만, Visibility에 바인딩 중 하나)을 노출하기 위해 뷰 모델을 변경하는 간단한 문제입니다하지 않습니다 가시성 속성, 또는 b) 우리의 부울을 가시성 열거 형으로 변환하는 값 변환기에 바인딩.

희망이 도움이됩니다. :)

관련 문제