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 C开发者_JS百科reationDate}" />
</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. I.e. 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.
精彩评论