0

動的パスのエンドポイントを取得し、それにオブジェクトを追加する方法を探しています-この種のパターンに似ています:

代替テキスト

赤い円は、指定されたパスの終点が配置されている場所です。これではなく、このようにパスが作成されることに注意してください。

<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"/>

データベースからパスデータを取得します。

提案/コメントはありますか?

PS。パスの終点にオブジェクト/画像 (移動または非移動) を配置しようとしています。

4

1 に答える 1

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上記ではimage \ buttonを非表示にしませんが、にバインドするVisibilityのは、a)ビューモデルを変更して可視性プロパティを公開するか、b)ブール値を可視性列挙型に変換する値コンバーターにバインドするだけです。 。

お役に立てれば!:)

于 2010-07-13T15:02:53.933 に答える