简体   繁体   English

UWP ListView将SelectedItem绑定到viewmodel中的属性

[英]UWP ListView bind SelectedItem to property in viewmodel

I'm trying to bind SelectedItem in a ListView to the property SelectedSection in the viewmodel. 我试图将ListView中的SelectedItem绑定到视图模型中的属性SelectedSection。 I'm getting the following error: "Invalid binding path 'ViewModel.SelectedSection' : Cannot bind type 'Model.Section' to 'System.Object' without a converter". 我收到以下错误:“无效的绑定路径'ViewModel.SelectedSection':如果没有转换器,则无法将'Model.Section'类型绑定到'System.Object'。 I'm currently binding the ListView's ItemSource to a list in the CurrentProject property. 我当前将ListView的ItemSource绑定到CurrentProject属性中的列表。 I tried making a converter, but I'm not quite sure how and what I'm suppose to convert. 我尝试制作一个转换器,但是我不确定如何转换。 I got the same error when I tried to just get the property I need using SelectedValue and SelectedValuePath. 当我尝试仅使用SelectedValue和SelectedValuePath获得所需的属性时,出现了相同的错误。

<ListView Name="SectionsListView"
                IsItemClickEnabled="True"
                ItemsSource="{x:Bind ViewModel.CurrentProject.Sections, Mode=OneWay}"
                SelectedItem="{x:Bind ViewModel.SelectedSection, Mode=TwoWay}">

ViewModel: ViewModel:

 private Section selectedSection = new Section();
 public Section SelectedSection
 {
     get { return selectedSection; }
     set { selectedSection = value; OnPropertyChanged(nameof(SelectedSection)); }
 }

 private Project currentProject;
 public Project CurrentProject
 {
     get { return currentProject; }
     set { currentProject = value; OnPropertyChanged(nameof(CurrentProject)); }
 }

Converter: 转换器:

 public object Convert(object value, Type targetType, object parameter, string language)
 {
     return value;
 }
 public object ConvertBack(object value, Type targetType, object parameter, string language)
 {
     return value as Section;
 }

If you had implemented this with the traditional {Bindings } markup you wouldn't have this issue, since the bindings are evaluated at runtime, while {x:Bind } is evaluated at compile time. 如果您使用传统的{Bindings}标记实现了此操作,则不会出现此问题,因为绑定是在运行时评估的,而{x:Bind}是在编译时评估的。

The situation here is that both ItemsSource and SelectedItem are properties of type Object and hence the problem arises when your Target tries to update the Source, due to the fact that you cannot assign a property of type Object to your (ViewModel.SelectedSection) property. 这里的情况是ItemsSource和SelectedItem都是Object类型的属性, 因此由于无法将Object类型的属性分配给您的(ViewModel.SelectedSection)属性因此当您的Target尝试更新Source时会出现问题。 The opposite doesn't throw any error since you your ViewModel.SelectedSection property can be implicitly casted to object. 相反,因为您的ViewModel.SelectedSection属性可以隐式转换为对象,所以不会引发任何错误。

One of the features of x:Bind, is that you can can cast your properties, like this for example: x:Bind的功能之一就是您可以强制转换属性,例如:

{x:Bind (x:Boolean)CheckBox.IsChecked, Mode=TwoWay}"

The problem is that since in your situation we are not dealing with one of the XAML intrinsic data types, you have to map onto your XAML namespace your ViewModel class, by including it on your XML namespace defined at the root of your page. 问题在于,由于在您的情况下我们不处理XAML固有数据类型之一,因此您必须将ViewModel类映射到XAML命名空间,方法是将其包含在页面根目录中定义的XML命名空间中。

xmlns:myviewmodel="using:......"

After including it, i think you can successfully cast it onto the desired reference type, without any compilation error, by performing casting like this: 包括它之后,我认为您可以通过执行如下的强制转换成功地将其强制转换为所需的引用类型,而没有任何编译错误:

<ListView Name="SectionsListView"
                IsItemClickEnabled="True"
                ItemsSource="{x:Bind ViewModel.CurrentProject.Sections, Mode=OneWay}"
                SelectedItem="{x:Bind (myviewmodel:ViewModel) ViewModel.SelectedSection, Mode=TwoWay}">

Or you can make some tweaks on your code and work with {Binding }, which honestly just simplifies entirely this process! 或者,您可以对代码进行一些调整并使用{Binding},说实话,这完全简化了整个过程!

Thanks for your response, I got it working by setting the datacontext to the viewmodel and used regular binding instead. 感谢您的答复,我通过将datacontext设置为viewmodel并使用常规绑定来使其正常工作。

<ListView Name="SectionsListView"
          DataContext="{x:Bind ViewModel}"
          ItemsSource="{Binding CurrentProject.Sections, Mode=OneWay}"
          SelectedItem="{Binding SelectedSection, Mode=TwoWay}">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM