【问题标题】:How to synchronize ListCollectionView.CurrentItem and ListView.SelectedItem?如何同步 ListCollectionView.CurrentItem 和 ListView.SelectedItem?
【发布时间】:2015-10-14 14:38:23
【问题描述】:

我有一个 ViewModel 包含一个 ListCollectionView 属性,一个 View 包含一个 ListView 控件,其 ItemsSource 属性是绑定到 ListCollectionView 的数据:

视图模型:

    public ListCollectionView Pacientes { get; set; }

    public IRepositório<Paciente> RepositórioPacientes { get; set; }


    // CONSTRUTOR
    public MainViewModel()
    {
        RepositórioPacientes = new PacienteRepositorioFake();

        Pacientes = (ListCollectionView)CollectionViewSource.GetDefaultView(RepositórioPacientes.Items);
    }
}

视图(大量精简):

    <ListView ItemsSource="{Binding Pacientes}"/>
    <Border DataContext="{Binding Pacientes/}">
        <!-- some controls displaying properties of Paciente entity -->
    </Border>

注意带有斜杠的Binding Pacientes/,试图绑定到Pacientes.CurrentItem

我的目的是提供一个主/详细视图,其中一个 ListView 显示所有项目,一个侧面板显示来自当前/选定项目的信息。

事实是:当我在 ListView 中选择一个元素时,我希望设置 Pacientes.CurrentItem,但显然不是!

所以我的问题是:如何通过选择数据绑定 ListBox 上的项目来设置 ListCollectionView.CurrentItem?

【问题讨论】:

  • 绑定控件(ListView)的选中项。

标签: wpf listview data-binding collectionviewsource listcollectionview


【解决方案1】:

设置IsSynchronizedWithCurrentItem属性:

<ListView ItemsSource="{Binding Pacientes}" IsSynchronizedWithCurrentItem="True"/>

【讨论】:

  • 哇,这么简单...我已经阅读了很多关于所有这些内容的内容,当然我也阅读过有关 IsSync... 的内容,但我还没有弄清楚 那个 i> 是它应该被使用的原因......我的错。非常感谢!
猜你喜欢
  • 2015-08-22
  • 2017-02-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-13
  • 2018-03-11
相关资源
最近更新 更多