带有列表的用户控件属性 - 有机会进行声明性定义吗?
我有一个具有以下属性的 UserControl:
public List<Rect> HotSpots
{
get { return (List<Rect>)GetValue(HotSpotsProperty); }
set { SetValue(HotSpotsProperty, value); }
}
public static readonly DependencyProperty HotSpotsProperty =
DependencyProperty.Register("HotSpots", typeof(List<Rect>), typeof(ImageViewPort), new FrameworkPropertyMetadata(HotSpotsChanged));
由于编译的 XAML(默认为 XAML 2006)不支持 2009 规范允许的泛型,我想知道是否有机会执行如下操作:
<WPF:ImageViewPort Grid.Row="1">
<WPF:ImageViewPort.HotSpots>
<Rect Location="0,0" Height="30" Width="50"></Rect>
<Rect Location="10,35" Height="30" Width="20"></Rect>
</WPF:ImageViewPort.HotSpots>
</WPF:ImageViewPort>
或者是我唯一的机会绑定像下面这样?
<WPF:ImageViewPort Grid.Row="1" HotSpots="{Binding Path=HotSpots}"/>
只是出于好奇,限制似乎是 XAML 对泛型的支持,因此编写 List 派生应该可以解决问题,不是吗?
I have a UserControl with the following Property:
public List<Rect> HotSpots
{
get { return (List<Rect>)GetValue(HotSpotsProperty); }
set { SetValue(HotSpotsProperty, value); }
}
public static readonly DependencyProperty HotSpotsProperty =
DependencyProperty.Register("HotSpots", typeof(List<Rect>), typeof(ImageViewPort), new FrameworkPropertyMetadata(HotSpotsChanged));
Since compiled XAML (XAML 2006 by default) doesn't support generics the way the 2009 specification allows, I wonder is there any chance of doing something like the following:
<WPF:ImageViewPort Grid.Row="1">
<WPF:ImageViewPort.HotSpots>
<Rect Location="0,0" Height="30" Width="50"></Rect>
<Rect Location="10,35" Height="30" Width="20"></Rect>
</WPF:ImageViewPort.HotSpots>
</WPF:ImageViewPort>
Or is my only chance a Binding like the following?
<WPF:ImageViewPort Grid.Row="1" HotSpots="{Binding Path=HotSpots}"/>
Just out of curiosity, the limitation seems to be XAML's support for generics, so writing a List derivate should do the trick, shouldn't it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您发布的 XAML 代码应该可以正常工作,您只需确保预先初始化
HotSpots
集合即可。只需在ImageViewPort
类的构造函数中初始化它The XAML code you posted should work fine, you just need to make sure the
HotSpots
collection is initialized beforehand. Just initialize it in the constructor of yourImageViewPort
class由于 XAML 不用于添加集合,而是用于设置集合,因此我必须创建一个包装 XAML 节点来初始化集合。由于 Collection 是通用的,我不能按原样执行此操作,而是必须创建一个包装器,如下所示:
然后在我的 XAML 中,我可以像这样设置它:
很简单,一旦您看到它工作=)
Since XAML isn't used to add to Collections but rather to setting them, I've had to create a wrapping XAML Node that initializes the Collection. Since the Collection is generic, I can't do this as it is, but have to create a wrapper like so :
In my XAML then, I can set it like so:
Easy enough, once you've seen it work =)