设置初始选定的项目绑定到ListView的SelectedItem属性时 [英] Setting the initial selected item when binding to a ListView's SelectedItem property

查看:603
本文介绍了设置初始选定的项目绑定到ListView的SelectedItem属性时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在里面我是用一个ListView允许用户选择一个项目从列表的Xamarin.Forms XAML页面。我已绑定ListView的SelectedItem属性在我的ViewModel属性,并能正常工作。一旦用户更改所选项目的属性我视图模型的更新。

I have a Xamarin.Forms xaml page in which I am using a ListView to allow the user to pick a single item out of a list. I have bound the ListView's SelectedItem property to a property on my ViewModel and this works fine. As soon as the user changes the selected item the property in my viewmodel updates as well.

不过,即使我最初设置在我的ViewModel属性,从列表中的一个值,当页面加载ListView的SelectedItem属性为null,进而设置视图模型属性设置为null为好。
我需要的是另外一个方向,我想在ListView最初选择,我在虚拟机属性所设置的项目。

However, even though I initially set the property in my ViewModel to one of the values from the list, when the page loads the ListView's SelectedItem property is null, which in turn sets the ViewModel property to null as well. What I need is the other direction, I want the ListView to initially select the item that i've set in the VM property.

我可以在隐藏文件中明确设置初始选定项的code编写额外的code一起砍一个解决方案,但是这引入了额外的特性和复杂性,是相当难看。

I can hack together a solution by writing extra code in the code behind file to explicitly set the initial selected item, but this introduces additional properties and complexity and is quite ugly.

什么是设置谁的选择的项目被绑定到视图模型属性ListView的初始选项的正确方法是什么?

What is the correct way to set the initial selected item of a ListView who's selected item is bound to a viewmodel property?

- 编辑 -

我被要求提供,我使用我的绑定code。
这是非常简单的标准:

I was asked to provide the code that I'm using for my binding. It's very simple, standard:

<ListView x:Name="myList" ItemsSource="{Binding Documents}" SelectedItem="{Binding SelectedDocument}">

这是设置为ListView的绑定上下文视图模型被实例化的之前创建,看起来像这样页:

the view model that is set as the binding context for the listview is instantiated before the page is created and looks like this:

public class DocumentSelectViewModel : ViewModelBase
    {
        private Document selectedDocument;

        public List<Document> Documents
    {
        get { return CachedData.DocumentList; }
    }
        public Document SelectedDocument { get {  return selectedDocument; } set { SetProperty(ref selectedDocument,value); }
public DocumentSelectViewModel () {
SelectedDocuement = CachedData.DocumentList.FirstOrDefault();
}
}

的SetProperty是简单的rasies事件INotifyPropertyChanged的,如果新的值是旧的,古典的结合code。

SetProperty is a function which simply rasies the INotifyPropertyChanged event if the new value is different from the old one, classical binding code.

推荐答案

我在XAML有点生疏,但并不需要做出具有约束力的双向?

I am a little rusty on XAML but don't you need to make the binding two-way?

例如

{ Binding SelectedDocument, Mode=TwoWay }

只要SelectedDocument属性更改引发INotifyPropertyChanged的事件,那么你应该得到预期的效果。

As long as the SelectedDocument property change raises the INotifyPropertyChanged event then you should get the desired effect.

这篇关于设置初始选定的项目绑定到ListView的SelectedItem属性时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆