【问题标题】:MVVM: Bind ListView.SelectedItem.Property to VM PropertyMVVM:将 ListView.SelectedItem.Property 绑定到 VM 属性
【发布时间】: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 的一部分。我可以在后面的代码中做到这一点,但我会尽可能地坚持模式。

标签: wpf listview mvvm binding


【解决方案1】:

SelectedItem 必须指向存在于ItemsSource 中的对象。您需要查看 SelectedValueSelectedValuePath 以了解您的场景。

【讨论】:

    猜你喜欢
    • 2014-09-29
    • 2011-08-26
    • 1970-01-01
    • 2012-03-08
    • 2020-04-19
    • 1970-01-01
    • 2016-07-06
    • 2016-09-26
    • 2011-08-23
    相关资源
    最近更新 更多