1
일부 특수 렌더링을 수행하는 WPF UIElement
을 만들려고합니다.WPF : 사용자 지정 UIElement에 대한 PointCollection 데이터 바인딩
문제는 데이터 바인딩이 PointCollection
에서 작동하지 않는다는 것입니다. 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);
}
}
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();
}
}
}
나는 그것을 시도했지만 작동하지 않았다. –
OnRender에서 여전히 null이 발생합니까? 샘플 프로젝트에 코드를 복사했고 코드를 변경했을 때 저에게 도움이되었습니다. –
사실, 그게 수정되었습니다. 내 질문을 게시하고 답변을 게시하는 사이에 다른 것을 엉망으로 만들었습니다. –