【发布时间】:2011-05-20 02:19:05
【问题描述】:
我正在使用以下 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>
我将 ListView(“文件”对象)绑定到一个 ViewModel,并将 SelectedItem 绑定到另一个 ViewModel 上的“文件”对象。这很好用,但现在我不需要绑定整个对象,而是绑定一个属性。 IE。而不是SelectedItem="{Binding Path=CurrentFile 我需要SelectedItem.FilePath="{Binding Path=FilePath。这是可能的还是 SelectedItem 绑定必须映射到 ListView 集合的相同对象类型?
【问题讨论】:
-
你的方案是什么?在我看来,您正在尝试完成一些奇怪的事情。
-
是的。我正在加载一个基于 Windows Media Player 的用户控件,该控件具有基于所选文件的自己的视图模型。此控件需要可重用,因此不能成为 mainViewModel 的一部分。我可以在后面的代码中做到这一点,但我会尽可能地坚持模式。