【发布时间】:2013-10-16 15:11:18
【问题描述】:
我想创建我自己的 UserControl(我们称之为“RectAtCoordinates”),它的工作原理与 Canvas 类似,但它应该:
-存储 (x,y) 整数坐标的集合
-在画布上为每个 (x,y) 对绘制矩形(具有任意选择的大小和填充)。 (x,y) 指定矩形的位置。
首先,我创建了简单的坐标类:
class Coordinates : DependencyObject
{
public static readonly DependencyProperty XProperty =
DependencyProperty.Register("X", typeof(int), typeof(Coordinates));
public static readonly DependencyProperty YProperty =
DependencyProperty.Register("Y", typeof(int), typeof(Coordinates));
public int X
{
get { return (int)GetValue(XProperty); }
set { SetValue(XProperty, value); }
}
public int Y
{
get { return (int)GetValue(YProperty); }
set { SetValue(YProperty, value); }
}
public Coordinates(int x, int y)
{
this.X = x;
this.Y = y;
}
}
这是我的 RectAtCoordinates UserControl (.xaml):
<UserControl x:Class="RectAtPoint.RectAtCoordinates"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<ItemsControl ItemsSource="{Binding Path=Coos, Mode=OneWay}">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
<Canvas Width="300" Height="300" Background="White"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate>
<DataTemplate>
<Rectangle Fill="Black" Width="50" Height="50"/>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemContainerStyle>
<Style>
<Setter Property="Canvas.Left" Value="{Binding Path=X}" />
<Setter Property="Canvas.Top" Value="{Binding Path=Y}" />
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
</UserControl>
最后是RectAtCoordinates 后面的代码:
public partial class RectAtCoordinates : UserControl
{
public static readonly DependencyProperty CoosProperty =
DependencyProperty.Register("Coos", typeof(Coordinates), typeof(RectAtCoordinates));
private ObservableCollection<Coordinates> Coos
{
get { return (ObservableCollection<Coordinates>)GetValue(CoosProperty); }
set { SetValue(CoosProperty, value); }
}
public RectAtCoordinates()
{
InitializeComponent();
Coos = new ObservableCollection<Coordinates>();
}
public void AddRectangleAtPosition(int x, int y)
{
Coos.Add(new Coordinates(x, y));
}
}
但是,在构建我的项目后,它崩溃了。我遇到 CLR20r3 问题。经过进一步检查,我将 RectAtCoordinates 构造函数更改为:
public RectAtCoordinates()
{
InitializeComponent();
try
{
Coos = new ObservableCollection<Coordinates>();
}
catch (Exception e)
{
MessageBox.Show(e.ToString());
}
}
得到了这个错误:
System.ArgumentException: 'System.Collections.ObjectModel.ObservableCollection`1[RectAtPoint.Coordinates]' 不是属性“Coos”的有效值。
在 System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, 对象值、PropertyMetadata 元数据、布尔值 coerceWithDeferredReference,布尔 coerceWithCurrentValue, OperationType operationType, Boolean isInternal)
在 System.Windows.DependencyObject.SetValue(DependencyProperty dp, 对象值)
在 RectAtPoint.RectAtCoordinates.set_Coos(ObservableCollection`1 值)在 c:...\RectAtCoordinates.xaml.cs:line 28
在 RectAtPoint.RectAtCoordinates..ctor() 中 c:...\RectAtCoordinates.xaml.cs:第 36 行
我是考虑 WPF、绑定和依赖属性的新手,所以请考虑到我可能犯了一些基本错误。我发现了许多类似的问题,但我无法完全理解解决方案,因此无法正确应用它们。
【问题讨论】:
-
我认为您需要为每个 dp 声明默认值。 msdn.microsoft.com/en-us/library/ms752914.aspx。在 Visual Studio 中,在您的类中键入 propdp 并按 Tab 2 次。它是依赖属性的 sn-p。
标签: c# wpf xaml user-controls observablecollection