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上面不会隐藏图像\按钮,但绑定到Visibility是一个简单的问题:a)更改我们的视图模型以公开可见性属性,或 b)绑定到将我们的布尔值转换为可见性枚举的值转换器.

希望这可以帮助!:)

于 2010-07-13T15:02:53.933 回答