【发布时间】:2013-06-06 09:29:10
【问题描述】:
我有一个名为 Switch 的 userControl,它有一个 List 类型的 DependancyProperty,名为 ImageList。 在我的 ViewModel 中,我创建了一个名为 imgList 的 ObservableCollection。 在我的 XAML 窗口中,我编写了以下行:
<loc:switch ImageList={Binding imgList}/>
最遗憾的是,这根本不起作用,并且 ImageList 没有收到请求的值。
ImageList 定义如下:
public static readonly DependancyProperty ImageListProperty = DependancyProperty.Register
("ImageList", typeof(List<ImageSource>), typeof(Switch));
public List<ImageSource> ImageList {
get { return (List<ImageSource>)GetValue(ImageListProperty); }
set { SetValue(ImageListProperty, value); }
}
viewModel 是正确的,如下所示:
<ComboBox ItemsSource={Binding imgList}/>
有趣的是,这可以正常工作:
<loc:switch>
<loc:switch.ImageList>
<BitmapImage UriSource="Bla.png"/>
<BitmapImage UriSource="Bla2.png"/>
</loc:switch.ImageList>
</loc:switch>
提前致谢!
【问题讨论】:
-
在你的用户控件中显示绑定和你的依赖属性代码
-
@blindmeis:刚刚添加,谢谢
-
@DHN:这有什么意义?重要的一点是 ImageList 保持不变并且没有得到 VM 中定义的集合
标签: wpf xaml data-binding dependency-properties