1

我想创建我自己的 UserControl(我们称之为“RectAtCoordinates”),它的工作原理与 Canvas 类似,但是它应该:

-存储(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,对象值)

在 c:...\RectAtCoordinates.xaml.cs:line 28 中的 RectAtPoint.RectAtCoordinates.set_Coos(ObservableCollection`1 值)

在 c:...\RectAtCoordinates.xaml.cs:line 36 中的 RectAtPoint.RectAtCoordinates..ctor()

我是考虑 WPF、绑定和依赖属性的新手,所以请考虑到我可能犯了一些基本错误。我发现了许多类似的问题,但我无法完全理解解决方案,因此无法正确应用它们。

4

2 回答 2

3

我认为你的问题在这里:

 public static readonly DependencyProperty CoosProperty =
            DependencyProperty.Register("Coos", typeof(Coordinates), typeof(RectAtCoordinates));

应该是:

 public static readonly DependencyProperty CoosProperty =
            DependencyProperty.Register("Coos", typeof(ObservableCollection<Coordinates>), typeof(RectAtCoordinates));

试试看 :)

=== 编辑 === 对于坐标。

我认为您可以执行以下操作:

      public void AddRectangleAtPosition(int x, int y)
            {
                Coos.Add(new Coordinates(x, y));
if (PropertyChanged != null)
                        {
                            PropertyChanged(this, new PropertyChangedEventArgs("Coos"));
                        }
            }

那么你的班级应该有:

public partial class RectAtCoordinates : UserControl, INotifyPropertyChanged

之后,您可以像我通常对 NotifyPropertyChanged 一样拥有一个区域,如下所示:

#region notifypropertychanged region

public event PropertyChangedEventHandler PropertyChanged;

protected virtual void OnPropertyChanged(string propertyName)
{
    PropertyChangedEventHandler handler = PropertyChanged;
    if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
protected bool SetField<T>(ref T field, T value, string propertyName)
{
    if (EqualityComparer<T>.Default.Equals(field, value)) return false;
    field = value;
    OnPropertyChanged(propertyName);
    return true;
}

#endregion

}

试一下 :)

于 2013-10-16T15:20:16.347 回答
-3

我终于找到了解决方案:数据上下文设置不正确。我不得不添加

this.DataContext = this;

到 UserControl 的构造函数或添加:

DataContext="{Binding RelativeSource={RelativeSource Self}}"

到 UserControl 的 xaml 定义。

于 2013-10-23T20:50:58.947 回答