在ListView中设置SelectedItem之前如何加载ItemsSource? [英] How to load ItemsSource before setting SelectedItem in ListView?

查看:223
本文介绍了在ListView中设置SelectedItem之前如何加载ItemsSource?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MVVM页面,其中包含ListView.我绑定了ItemSourceSelectedValue,但是第一次它调用SelectedValue的转换器,然后加载ItemSource.

I have a MVVM page that contains a ListView. I bind ItemSource and SelectedValue, but first time it calls converter for SelectedValue then loads ItemSource.

<ListView x:Name="ListViewSurahs"  
              ItemsSource="{Binding MyItems}"
              FlowDirection="LeftToRight"
              Grid.Column="2"
              Grid.Row="4"
              VerticalAlignment="Top"
              HorizontalAlignment="Left"
              HorizontalContentAlignment="Center"
              SelectionMode="Single"
              ScrollViewer.VerticalScrollBarVisibility="Auto"
              DisplayMemberPath="Name"
              SelectedValuePath="ID"
              SelectedValue="{Binding Source={StaticResource CurrentInfo},
                                      Path=Instance.ID,Mode=OneWay}"
              ShowsScrollingPlaceholders="False" />

因为我输了SelectedItem并且没有选择任何物品.我应该怎么做才能先加载ItemsSource?

because of that I lose SelectedItem and no Items get selected. what should I do to load ItemsSource first?

推荐答案

首先,您可以尝试在代码隐藏中键入ItemsSource.为此,您必须在xaml文件中添加UserLoaded属性.但是也许我们需要查看您的代码背后和viewmodel.无论如何,您都应尝试将 SelectedValue 绑定模式从 OneWay 更改为 TwoWay .

Firstly you can try to type your ItemsSource in codebehind. You have to add an UserLoaded property in your xaml file to do this. But maybe we need to see your codebehind and viewmodel. Anyway you should try to change your SelectedValue binding mode OneWay to the TwoWay.

在那之后,您应该注意自己的装订风格.您必须在 viewmodel 中完成大部分开发进度,然后您可以使用 get-set 从xaml代码背后(.cs)调用您的viewmodel.因此,您将拥有一个非常干净的绑定结构.

After that you should watch your binding style. You have to complete most of your development progress in your viewmodel, and after that you could just call your viewmodel from your xaml codebehind (.cs) with get-set. So, you will have a very clean binding structure.

通过这种方式,您可以输入以下内容,而不是您的

In this way you can type as follows instead of yours,

    SelectedValue="{Binding Model.BlaBla, Mode=TwoWay}"

在此处模型中已定义并在xaml文件(.cs)的代码隐藏中调用了该模型.例如,在您的公开密封的局部类

in here Model is defined and called in your codebehind of xaml file (.cs). For example in top of your public sealed partial class

    public YourViewModelName Model { get; set; }

,并且在同一文件中,公开的YourXamlName()

and in the same file public YourXamlName()

    Model = new YourViewModelName();

这是一个快速的答案,我不确定.但是你应该试一试.

It is a quickly answer and I am not sure. But you should give a shot.

祝你好运.

这篇关于在ListView中设置SelectedItem之前如何加载ItemsSource?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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