带有列表的用户控件属性 - 有机会进行声明性定义吗?

发布于 2024-12-27 20:23:43 字数 978 浏览 4 评论 0原文

我有一个具有以下属性的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

说谎友 2025-01-03 20:23:43

您发布的 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 your ImageViewPort class

欢你一世 2025-01-03 20:23:43

由于 XAML 不用于添加集合,而是用于设置集合,因此我必须创建一个包装 XAML 节点来初始化集合。由于 Collection 是通用的,我不能按原样执行此操作,而是必须创建一个包装器,如下所示:

public class HotSpotList : List<Rect> {}

然后在我的 XAML 中,我可以像这样设置它:

<WPF:ImageViewPort.HotSpots>
  <HotSpotList>
   <Rect Location="0,0" Height="30" Width="50"></Rect>
   <Rect Location="10,35" Height="30" Width="20"></Rect>
  </HotSpotList>
</WPF:ImageViewPort.HotSpots>

很简单,一旦您看到它工作=)

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 :

public class HotSpotList : List<Rect> {}

In my XAML then, I can set it like so:

<WPF:ImageViewPort.HotSpots>
  <HotSpotList>
   <Rect Location="0,0" Height="30" Width="50"></Rect>
   <Rect Location="10,35" Height="30" Width="20"></Rect>
  </HotSpotList>
</WPF:ImageViewPort.HotSpots>

Easy enough, once you've seen it work =)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文