抱歉我的英语不好。我试图在列表视图的项目源中绑定图像列表:
ItemsSource="{x:Bind ViewModel.Images,Mode=TwoWay}"
In ViewModel I have a list of Images :
public List<Image> Images
{
get { return _images; }
set
{
_images = value;
base.RaisePropertyChanged();
}
}
如果绑定模式设置为TwoWay,则会出现以下错误:
Invalid binding path 'View Model.Reports': You can not associate the type 'System.Collections.Generic.List (posytron.epart.uwp.Models.DOM.Report)' to 'System.Object' without a converter
如果我将绑定模式设置为OneWay就可以了!
我已经开发了一个转换器,用于将布尔值转换为可见性属性。
转换器应包含哪些内容?有人能帮我吗?为什么我有这个错误?
谢谢!
答案 0 :(得分:0)
I'm not using Tempalte 10, only a normal UWP app to test your problem, and I think I've reproduced your problem.
The error message is a little different possible due to the Model of Template 10, but I think the error is the same.
If I set binding mode to OneWay it works!
If you use OneWay mode of {x:Bind}, your ListView
's ItemSource
is the binding target, and your List<Image> Images
in the ViewModel is the binding source. Obviously your source is a list of Image
controls. The type of ItemSource
is System.Object, and the binding source here is a List of type Image control. By default, a data item is displayed in the ListView as the string representation of the data object it's bound to. So when xaml can't recognize this string, it will directly use implementation of ToString method to return the type name and show it on the item. In a word, ItemSource
as binding target has done the work of converting types.
But when using TwoWay mode, ItemSource is binding target and also the binding source, so is the List<Image> Images
. It works well in the pattern "ItemSource = binding target", the reason I've explained upper, but if the "ItemSource = binding source", it will not automatically convert the System.Object type to your List type. Also you can say, your doesn't provide a converter to converter System.Object type to your needed type.
But I don't think you need TwoWay binding here. TwoWay binding Updates either the target or the source object when either changes. When the binding is created, the target property is updated from the source. From your code, I can't see you can change Image controls in the Items to update the source List. If you just want to dynamic add or remove or edit the items in the ListView, you can use OneWay mode here and change the List in the code behind.