WPF/C#:获取动态路径的端点并向其添加对象

发布于 2024-09-09 11:01:06 字数 783 浏览 8 评论 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"/>

我从数据库获取路径数据。

有什么建议/意见吗?

附言。我试图将对象/图像(移动或不移动)放置在路径的端点。

I am looking for way to get the endpoint of a dynamic path and add on object to it - similar to this kind of pattern:

alt text

where the red circle is where the endpoint of the given path is located. take note that the path is created thus, instead of this:

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

I made use of this:

<Path Stroke="Black" x:Name="path1" Data="{Binding MyProperty1}"  Margin="0" StrokeThickness="4"/>

where I get the path data from a database.

Any suggestions/comments?

PS. I am trying to place an object/image (moving or non-moving) at the endpoint of the path.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

摇划花蜜的午后 2024-09-16 11:01:06

嗯,重申我上面的评论,想必您有类似的东西,

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) 绑定到将布尔值转换为可见性枚举的值转换器。

希望这有帮助! :)

Hm, reiterating my comments above, presumably you have something like this

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

simply extend this model by implementing INotifyPropertyChanged, and creating an explicit property for last point in path,

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);
        }
    }

}

the only thing worth noting is firing the property changed event when the collection changes. this notifies WPF that the model has changed.

Now, in Xaml land,

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

Ok, so IsEnabled above won't hide the image\button, but binding to Visibility is a simple matter of either a) changing our view model to expose a visibility property, or b) binding to a value converter that converts our boolean to a visibility enum.

Hope this helps! :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文