无法将ObservableCollection绑定到列出来自xaml的dp [英] Cannot bind ObservableCollection to List dp from xaml
问题描述
在我的ViewModel中,我创建一个名为imgList的ObservableCollection。
在我的XAML窗口中,我写下列行:
< loc:switch ImageList = {Binding imgList} />
为了我最大的遗憾,这根本不起作用,ImageList没有收到请求的值。 / p>
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>
提前感谢
如何预期 ObservableCollection
type(imgList)的传入值将与 List<>
type(Switch.ImageList)?
请使您的类型兼容。
一种方法是将 ImageListProperty
重新声明为 IList
类型为 ObservableCollection<> / code>实现
IList
界面。
在WPF中,绑定将会在目标属性的类型相同时出现或基本类型到源值。 ObservableCollection
不从列表<>
中导出。
编辑
您的最后一个示例停止工作,因为 IList
不会隐式实例化作为接口),而没有通过XAML提供给它的显式集合类型。将 ImageListProperty
更改为 IList
(不是 IList< T>
)
你应该以这种方式改变....
< loc:switch xmlns:coll =clr-namespace:System.Collections; assembly = mscorlib>
< loc:switch.ImageList>
< coll:ArrayList>
< BitmapImage UriSource =Bla.png/>
< BitmapImage UriSource =Bla2.png/>
< / coll:ArrayList>
< / loc:switch.ImageList>
< / loc:switch>
这是因为 ArrayList
是一个具体的XAML实现 IList
的序列化集合。而 ObservableCollection<>
也是简单的 IList
。
I have a userControl named Switch which has a DependancyProperty of type List, named ImageList. In my ViewModel, I create a ObservableCollection named imgList. In My XAML window, I write the following line:
<loc:switch ImageList={Binding imgList}/>
To my greatest regret, this does not work at all and ImageList does not recieve the requested values.
ImageList is defined as the following:
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); }
}
The viewModel is correct, as the following works:
<ComboBox ItemsSource={Binding imgList}/>
It is interesting to note that this works correctly:
<loc:switch>
<loc:switch.ImageList>
<BitmapImage UriSource="Bla.png"/>
<BitmapImage UriSource="Bla2.png"/>
</loc:switch.ImageList>
</loc:switch>
Thanks in advance!
How do you expect an incoming value of ObservableCollection<>
type (imgList) would match with List<>
type (Switch.ImageList)?
Please make your types compatible.
One way is to redeclare ImageListProperty
to IList<>
type as ObservableCollection<>
implements IList
interface.
In WPF Binding would wouk when the target property's type is same or a base type to the source value. ObservableCollection
doesnt derive from List<>
.
EDIT
Your last example stops working because IList<>
isnt instantiated implicitly (being an Interface) without explicit collection type supplied to it via XAML. Change ImageListProperty
to IList
(Not IList<T>
).
You should it change this way....
<loc:switch xmlns:coll="clr-namespace:System.Collections;assembly=mscorlib">
<loc:switch.ImageList>
<coll:ArrayList>
<BitmapImage UriSource="Bla.png"/>
<BitmapImage UriSource="Bla2.png"/>
</coll:ArrayList>
</loc:switch.ImageList>
</loc:switch>
This works because ArrayList
is a concrete XAML serialized collection that implements IList
. And ObservableCollection<>
also does plain IList
.
这篇关于无法将ObservableCollection绑定到列出来自xaml的dp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!