C# WPF:为自定义UIElement数据绑定PointCollection

C# WPF:为自定义UIElement数据绑定PointCollection,c#,wpf,data-binding,uielement,C#,Wpf,Data Binding,Uielement,我正在尝试制作一个WPFUIElement,它执行一些特殊的渲染 问题是我无法让数据绑定为点集合工作。在GraphElement的OnRender()中,Points属性为null,这对我来说没有任何意义 class GraphElement : UIElement { public static readonly DependencyProperty PointsProperty = DependencyProperty.Register( "Points",

我正在尝试制作一个WPF
UIElement
,它执行一些特殊的渲染

问题是我无法让数据绑定为
点集合工作。在
GraphElement
OnRender()
中,Points属性为null,这对我来说没有任何意义

class GraphElement : UIElement
{
    public static readonly DependencyProperty PointsProperty = DependencyProperty.Register(
        "Points",
        typeof(PointCollection),
        typeof(GraphElement),
        new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.AffectsRender));            

    public PointCollection Points
    {
        get { return (PointCollection)GetValue(PointsProperty); }
        set { SetValue(PointsProperty, value); }
    }

    protected override void OnRender(DrawingContext drawingContext)
    {
        base.OnRender(drawingContext);

        //Points is null
        Debug.Assert(Points != null);
    }

}




使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Windows;
使用System.Windows.Controls;
使用System.Windows.Data;
使用System.Windows.Documents;
使用System.Windows.Input;
使用System.Windows.Media;
使用System.Windows.Media.Imaging;
使用System.Windows.Navigation;
使用System.Windows.Shapes;
命名空间TestApp
{
/// 
///MainWindow.xaml的交互逻辑
/// 
公共部分类主窗口:窗口
{
公共主窗口()
{
var testViewModel=新的testViewModel();
testViewModel.ViewModelPoints=新点集合();
添加(新点(0.0,0.0));
DataContext=testViewModel;
初始化组件();
}
}
}

GraphElement
上的继承从
UIElement
更改为
FrameworkElement
(后者依次继承
UIElement
)。这里有
DataContext
dp等

class GraphElement : FrameworkElement
{
    //...
}

将代码复制到示例项目中(将UIElement更改为FrameworkElement),并且OnRender中的Points不为null。上传了这个项目

在OnRender中是否仍然得到null?我将您的代码复制到一个示例项目中,当我实际更改它时,它对我有效,这确实修复了它。在发布我的问题和你发布答案之间,我搞砸了其他事情。
<Window x:Class="TestApp.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TestApp"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <Canvas>
        <local:GraphElement Points="{Binding Path=ViewModelPoints}"/>
    </Canvas>
</Grid>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace TestApp
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            var testViewModel = new TestViewModel();
            testViewModel.ViewModelPoints = new PointCollection();
            testViewModel.ViewModelPoints.Add(new Point(0.0, 0.0));
            DataContext = testViewModel;
            InitializeComponent();
        }
    }
}
class GraphElement : FrameworkElement
{
    //...
}