Reputation: 4167
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?
Upvotes: 2
Views: 661
Reputation: 4167
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 =)
Upvotes: 1
Reputation: 292555
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
Upvotes: 1