WPF:如何定义在xaml中使用的集合

时间:2010-05-27 06:29:55

标签: wpf xaml collections

我想定义类似这样的内容

<myCustomControl>
  <myCustomControl.Images>
     <Image
        Source="{StaticResource LockedIcon16}" />
     <Image
        Source="{StaticResource UnlockedIcon16}"/>
  <myCustomControl.Images>
<myCustomControl/>

我需要使用哪些属性定义才能获得该集合(图像)?

1 个答案:

答案 0 :(得分:1)

System.Windows.Controls.Image应该这样做。

没有测试过,但它应该可以工作。

public class myCustomControl {
  //...
    public ObservableCollection<Image> Images {
        get { return (ObservableCollection<Image>)GetValue(ImagesProperty); }
        set { SetValue(ImagesProperty, value); }
    }

    // Using a DependencyProperty as the backing store for Images.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty ImagesProperty =
        DependencyProperty.Register("Images", typeof(ObservableCollection<Image>), typeof(myCustomControl), new PropertyMetadata(null));
}