开发者

MVVM: Bind ListView.SelectedItem.Property to VM Property

开发者 https://www.devze.com 2023-01-28 15:57 出处:网络
I am using the following ListView: <ListView DataContext=\"{StaticResource mainViewModel}\" ItemsSource=\"{Binding Items.View}\" IsSynchronizedWithCurrentItem=\"True\" SelectedItem=\"{Binding Path

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消