MVVM: Bind ListView.SelectedItem.Property to VM Property
I am using the following ListView:
<ListView DataContext="{StaticResource mainViewModel}" ItemsSource="{Binding Items.View}" IsSynchronizedWithCurrentItem="True" SelectedItem="{Binding Path=CurrentFile, Source={StaticResource anotherViewModel}, Mode=OneWayToSource}">
<ListView.View>
<GridView>
<GridViewColumn Header="File Path" DisplayMemberBinding="{Binding FilePath}" />
<GridViewColumn Header="Creation" DisplayMemberBinding="{Binding CreationDate}" />
</GridView>
</ListView.View>
</ListView>
I am binding the ListView (of "File" objects) to one ViewModel and the SelectedItem to a "File" object on another ViewModel. This works fine but now I need to not bind the whole object, but one property. Ie instead of something like SelectedItem="{Binding Path=CurrentFile
I need SelectedItem.FilePath="{Binding Path=FilePath
. Is this possible or does SelectedItem binding have to map to the same object type of the ListView collection?
SelectedItem
must point to an object present in the ItemsSource
. You need to look at SelectedValue
and SelectedValuePath
for your scenario.
上一篇: SelectedItem属性未更新