Canvas と同様に機能する独自の UserControl (「RectAtCoordinates」と呼びましょう) を作成したいと思いますが、次のようにする必要があります。
-(x,y)整数座標のコレクションを保存
- (x,y) ペアごとにキャンバス上に (任意のサイズと塗りつぶしで) 四角形を描画します。(x,y) 長方形の位置を指定します。
まず、単純なCoordinates クラスを作成しました。
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、ブール値 isInternal) で
System.Windows.DependencyObject.SetValue (DependencyProperty dp、オブジェクト値) で
RectAtPoint.RectAtCoordinates.set_Coos(ObservableCollection`1 value) で c:...\RectAtCoordinates.xaml.cs:28 行目
RectAtPoint.RectAtCoordinates..ctor() で c:...\RectAtCoordinates.xaml.cs:36 行目
私は WPF、バインディング、および依存関係プロパティを検討する初心者なので、いくつかの基本的な間違いを犯した可能性があることを考慮してください。私は多くの同様の問題を見つけましたが、解決策を完全に理解できなかったため、適切に適用できませんでした。