我想利用Polyline进行二次开发,因此在自定义用户控件中使用了Polyline
<UserControl x:Class="UserControl.Views.UserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:UserControl.Views"
mc:Ignorable="d"
d:DesignHeight="450" d:DesignWidth="800">
<UserControl.Resources>
<local:PointConverter x:Key="pc">local:PointConverter>
UserControl.Resources>
<Grid>
<Polyline x:Name="pl" Points="{Binding Points,RelativeSource={ RelativeSource AncestorType={x:Type local:UserControl1},Mode=FindAncestor},Converter={StaticResource pc}}">Polyline>
Grid>
UserControl>
后端C#代码
public ObservableCollection Points
{
get { return (ObservableCollection)GetValue(PointsProperty); }
set { SetValue(PointsProperty, value); }
}
// Using a DependencyProperty as the backing store for Points. This enables animation, styling, binding, etc...
public static readonly DependencyProperty PointsProperty =
DependencyProperty.Register("Points", typeof(ObservableCollection), typeof(TrainLine));
当我尝试像Polyline一样使用UserControl1的时候产生了问题
<td:UserControl1 x:Name="uc1" Points="{Binding MyPoints}" >td:UserControl1>
上方代码MyPoints声明
private ObservableCollection<Point> _points = new ObservableCollection();
public ObservableCollection MyPoints
{
get {return _points; }
set { SetProperty(ref _points, value); }
}
当我通过逻辑代码调用MyPoint.Add()添加Point时,Point能按照预想在UserControl1的Points也进行同步,但是UserControl1中的Polyline却不能进行同步。
我的理解是Points(Polyline)绑定了Points(UserControl),Points(UserControl)又绑定了MyPoints,因此MyPoints的改变会传递到最后的Points(Polyline),但很显然,实际情况并不是这样。我最终希望的是,封装一个能像Polyline一样使用的控件。
最近刚开始学习WPF,也不知是不是从最开始实现思路就出现了问题,还是中间忽略了什么细节问题,希望各位帮帮忙!