代码之家  ›  专栏  ›  技术社区  ›  Adam Tegen

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

  •  1
  • Adam Tegen  · 技术社区  · 14 年前

    我想做一个WPF UIElement 做一些特殊的渲染。

    问题是我不能让数据绑定为 PointCollection . 内部 OnRender() GraphElement ,Points属性为空,这对我没有任何意义。

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

    class TestViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
    
        private PointCollection _viewModelPoints;
        public PointCollection ViewModelPoints
        {
            get { return _viewModelPoints; }
            set
            {
                _viewModelPoints = value;
                if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("ViewModelPoints"));
            }
        }
    }
    

    <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();
            }
        }
    }
    
    1 回复  |  直到 11 年前
        1
  •  2
  •   Dave Clemmer manu    11 年前

    更改继承 GraphElement UIElement FrameworkElement (反过来又继承了 元素 ). 你就在这里 DataContext dp等。

    class GraphElement : FrameworkElement
    {
        //...
    }
    

    将代码复制到示例项目(将UIElement更改为FrameworkElement)中,并且在OnRender中点不为空。已上载项目 here .