【问题标题】:WPF MVVM Listview itemsource binding with list in a class propertyWPF MVVM Listview itemssource 与类属性中的列表绑定
【发布时间】:2020-05-17 18:45:58
【问题描述】:

我有一个包含两个部分的页面: 带有 itemsource 的 Listview 与一个类绑定,ObservableCollection “Patients”加载了“Patient”类。 在 Listview 下是与选定项 Patient 绑定的文本框。除了在 selection_changed 中滚动到所选项目之外,一切都无需在页面中编写任何代码即可工作。

第二个列表视图必须显示来自所选“患者”的详细信息“访问”。

应用程序在 MVVM 框架中工作,其 Viewmodel 包含页面的属性。

问题是使两个ListView之间的关系。我首先尝试在 NotifyPropertyChanged 事件中构建第二个列表“访问”:

            if (Patient.ID > 0)
            {
                LoadVisite(Patient.ID); // fill the details list "Visites"
                NotifyPropertyChanged("Visites");

            }

选择“患者”时不显示延迟。

我尝试了另一种解决方案,将详细信息列表插入到大师班“像这样的病人:

    public Class Patient
    ...
    public ObservableCollection<ClsVisite> Visites
    {
        get
        {
            return _visites;
        }
        set
        {
            _visites = value;
        }
    }
            // WDABase class to open the database and load data connection 
            WDABase wd = new WDABase();
            wd.LoadListeVisites(ID, _visites); //ID is the relation beween the two tables


        }

    }

现在我尝试像这样在 XAML 中创建 Listview 详细项目源:

     <ListView Name="ListeVisites" ItemsSource="{Binding Path=Patient.Visites}" SelectedItem="{Binding Visite}">

没有显示细节。

我发现的唯一解决方案是像这样在主 Listview 的 selection_changed 事件中添加一些代码(在这种情况下,listviews 在两个不同的框架中):

    private void ListePatients_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
    {
        ListePatients.ScrollIntoView(ListePatients.SelectedItem);
        if(ListePatients.SelectedItem != null)
        {
            var w1 = ((System.Windows.FrameworkElement)App.Current.MainWindow.Content).Parent;
            Frame pageVisite = (w1 as MainWindow).Visit;
            var w2 = (System.Windows.FrameworkElement)pageVisite.Content;
            ListView Lv = (w2 as Visite).ListeVisites;
            Lv.ItemsSource = (ListePatients.SelectedItem as ClsPatient).Visites; 

        }

    }

它的工作原理,但是否有另一种优雅的解决方案来绑定细节 Listview 的 itemsource ? 谢谢你帮助我。 让-玛丽

【问题讨论】:

    标签: wpf listview binding master-detail


    【解决方案1】:

    我找到了解决问题的方法。

    未显示详细信息的原因是用于列表视图的框架与主框架不通用。 我发现当我显示详细信息框架时,“患者”对象仅包含空值。我不知道为什么,但我阅读了有关共享 ViewModel 的信息,现在我在 App.xaml 中使用了一个常见的,我不需要在代码中为详细信息 Listview 指定 itemsource。我输入了 XAML itemsource={Binding Patient.Visites},一切正常。

    【讨论】:

      猜你喜欢
      • 2012-01-08
      • 1970-01-01
      • 2016-03-28
      • 1970-01-01
      • 2018-03-24
      • 2011-01-14
      • 2014-12-08
      • 2011-06-05
      • 1970-01-01
      相关资源
      最近更新 更多