DaveO
DaveO

Reputation: 1989

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. 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?

Upvotes: 0

Views: 4418

Answers (1)

Kent Boogaart
Kent Boogaart

Reputation: 178810

SelectedItem must point to an object present in the ItemsSource. You need to look at SelectedValue and SelectedValuePath for your scenario.

Upvotes: 3

Related Questions