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" />
because of that I lose SelectedItem
and no Items get selected. what should I do to load ItemsSource
first?
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.
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}"
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; }
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.
Good luck.
来源:https://stackoverflow.com/questions/40741754/how-to-load-itemssource-before-setting-selecteditem-in-listview