本文介绍了WPF ListView数据绑定-具有实体集合的实体集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,这是我的第一个WPF应用程序,因此与C#Windows窗体相比,我在这里学习了很多新概念. 我已经审查了 其他帖子,但没有看到我认为的答案.

First of all, this is my first WPF application so I have been learning a lot of new concepts here as compared to C# Windows Forms.  I've reviewed other posts and didn't see what I believed to be an answer.

 

I有一个WPF绑定问题. 我正在使用MVVM编写WPF应用程序,该应用程序使用WCF服务从Oracle数据库中获取数据并以ObservableCollection的形式返回数据.我的实体类.  (注意:自从我写了我自己的实体类 我们正在使用Oracle.我意识到NHibernate在那里,但是我还没有对它进行太仔细的研究,因为所有这一切对我来说都是新的,编写代码现在可以帮助我学习.)我有一个Section类和SectionData类. 在这篇文章中,为简单起见,我在这里更改了名称.

 I have a WPF binding question. I am writing a WPF application using MVVM that uses WCF services to fetch data from an Oracle database and return the data as an ObservableCollection<> of my Entity classes.  (Note: I have written my own entity classes since we are using Oracle.  I realize NHibernate is out there, but I haven't investigated it too closely as all of this is new to me and writing the code helps me learn right now.)  I have a Section class and SectionData class. In this post, I have changed the names here for simplicity.

Section类具有以下属性:int Id,double BeginPoint,double EndPoint,字符串BeginDescription,字符串EndDescription,ObservableCollection< SectionData>. SectionDataCollection等.

The Section class has the properties: int Id, double BeginPoint, double EndPoint, string BeginDescription, string EndDescription, ObservableCollection<SectionData> SectionDataCollection, etc.

SectionData类具有以下属性:int SectionId,int Year,string Condition1,字符串Condition2,等等.

因此,基本上,一个Section具有SectionData的集合. "> 存在SectionData类是为了包含该数据收集年份的Section的条件数据.用户需要与Sections及其SectionData的集合进行交互.

So basically a Section has a collection of SectionData. The SectionData class exists in order to contain condition data for the Section for the Years the data has been collected.  The user needs to interact with a collection of Sections and their SectionData.

在我的SectionViewModel类中,我有一个ObservableCollection< SectionData>属性称为SectionCollection.  SectionView类绑定到此属性以在ListView中显示信息.

In my SectionViewModel class, I have an ObservableCollection<SectionData> property called SectionCollection. The SectionView class binds to this property to display information in a ListView.

这是我的问题,我可以让ListView显示一个通过执行以下操作可以毫无问题地列出部分:

SectionViewModel .cs:

 SectionViewModel.cs:

此处是Section实体集合的属性. 每个Section包含一个ObservableCollection of SectionData.

Here’s the property for the Collection of Section entities. Each Section contiains an ObservableCollection of SectionData.

public ObservableCollection < Section > SectionCollection

publicObservableCollection<Section> SectionCollection

{

获取 {返回 _sectionCollection; }

      get { return _sectionCollection; }

设置

      set

{

      {

       如果(_sectionCollection == )

             if (_sectionCollection == value)

              返回;

                    return;

       _sectionCollection = ;

             _sectionCollection = value;

       base .OnPropertyChanged("SectionCollection" );

             base.OnPropertyChanged("SectionCollection");

}

      }

}

 

出于测试目的,我对SectionViewModel的构造函数进行硬编码,以请求WCF服务提供2006年和2008年的所有部分 并使用以下结果填充我的SectionCollection属性:

For testing purposes, I’ve hard-coded the constructor of SectionViewModel to request all sections for the years 2006 and 2008 from the WCF Service and populate my SectionCollection property with the results like so:

ServiceReference1. ServiceClient serviceClient = ServiceReference1. ServiceClient ();

ServiceReference1.ServiceClient serviceClient =new ServiceReference1.ServiceClient();

SectionCollection = serviceClient.GetSections(

] ObservableCollection < int > {2006,2008});

                          new ObservableCollection<int> { 2006, 2008 });

 

 

SectionView.xaml:

< UserControl x : =" WpfApplicationTest.SectionView"

       xmlns =" http://schemas .microsoft.com/winfx/2006/xaml/presentation;

            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

       xmlns : x =" http://schemas.microsoft.com/winfx/2006/xaml"

            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

       xmlns : mc =" http://schemas.openxmlformats.org/markup-compatibility/2006"

            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

       xmlns : d =" http://schemas.microsoft.com/expression/blend/2008"

            xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

       xmlns : 模型 =" clr-namespace:Entities; assembly = Entities"

            xmlns:model="clr-namespace:Entities;assembly=Entities"

       mc : 不可思议 =" d"

            mc:Ignorable="d"

       d : DesignHeight ="196" d : DesignWidth ="548">

            d:DesignHeight="196" d:DesignWidth="548">

 

< ListView 名称 =" listViewSectionData" ItemsSource =""{ 绑定 SectionCollection }>

<ListView Name="listViewSectionData"ItemsSource="{Binding SectionCollection}">

< ListView.View >

;       < GridView >

           <GridView>

 <!-我知道这不是显示年份的方法,但不知道如何进行. ->

 <!-- I know that this isn't the way to display the year, but don't know how to do it. -->

;           < GridViewColumn 页眉 =" 年" DisplayMemberBinding =""{ 绑定 SectionDataCollection .SurveyYear}". />

               <GridViewColumn Header=" Year " DisplayMemberBinding="{Binding SectionDataCollection.SurveyYear}" />

 

;           < GridViewColumn 页眉 =" 起点 DisplayMemberBinding =""{ 绑定 BeginPoint }"//>

               <GridViewColumn Header=" Begin Point " DisplayMemberBinding="{Binding BeginPoint}"/>

;          < GridViewColumn 标题 ="端点 DisplayMemberBinding =""{ 绑定 EndPoint }"/>

               <GridViewColumn Header=" End Point " DisplayMemberBinding="{Binding EndPoint}"/>

;           < GridViewColumn 页眉 =" 开始说明 DisplayMemberBinding =""{ 绑定 BeginDescription }"//>

               <GridViewColumn Header=" Begin Description " DisplayMemberBinding="{Binding BeginDescription}"/>

;           < GridViewColumn 页眉 =" 结束说明 DisplayMemberBinding =""{ 绑定 EndDescription }"//>

               <GridViewColumn Header=" End Description " DisplayMemberBinding="{Binding EndDescription}"/>

;          < GridViewColumn 标题 =" Condition1 DisplayMemberBinding =""{ 绑定 SectionDataCollection .Condition1 }" />

               <GridViewColumn Header=" Condition1 " DisplayMemberBinding="{Binding SectionDataCollection .Condition1}" />

 < GridViewColumn 标头 ="条件2 " DisplayMemberBinding =""{ 绑定 SectionDataCollection .Condition2 }" />

 <GridViewColumn Header=" Condition2 " DisplayMemberBinding="{Binding SectionDataCollection .Condition2}" />

;       </ GridView >

           </GridView>

;   </ ListView.View >

      </ListView.View>

</ ListView >

</ UserControl >

MainWindow.xaml:

< 窗口 x : =" WpfApplicationTreeViewTest.MainWindow"

  xmlns =" http://schemas .microsoft.com/winfx/2006/xaml/presentation;

       xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

  xmlns : x =" http://schemas.microsoft.com/winfx/2006/xaml"

       xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

  xmlns : 本地 =" clr-namespace:WpfApplicationTest"

       xmlns:local="clr-namespace:WpfApplicationTest"

  xmlns : 视图模型 =" clr-namespace:WpfApplicationTest.ViewModel"

       xmlns:viewmodel="clr-namespace:WpfApplicationTest.ViewModel"

  标题 ="MainWindow" 高度 ="472" 宽度 ="616"

       Title="MainWindow" Height="472" Width="616"

  SizeToContent ="宽度">

       SizeToContent="Width">

 

< Window.Resources >

;    <!-将SectionView绑定到MainWindow构造函数中定义的SectionViewModel. ->

       <!-- Bind a SectionView to the SectionViewModel defined in the MainWindow constructor. -->

;    < DataTemplate DataType =&"; { x : 类型 viewmodel : SectionViewModel }">

       <DataTemplate DataType="{x:Type viewmodel:SectionViewModel}">

;       < 本地 : SectionView />

           <local:SectionView />

;    </ DataTemplate >

       </DataTemplate>

</ Window.Resources > 跨度>

 

< 网格 >

< ContentControl 内容 =&; { 绑定 }" />

<ContentControl Content="{Binding}" />

</ 网格 >

</ 窗口 >

 

MainWindow.xaml.cs:

public MainWindow()

{

InitializeComponent();

      InitializeComponent();

.DataContext = SectionViewModel ((1,0.0,0.9," BEGIN" " END” )));

      this.DataContext = new SectionViewModel(newSection(1, 0.0, 0.9, "BEGIN", "END”));

}

 

 

那不是我想要的. 我想在ListView中显示以下列:

That is not what I want though. I want to display the following columns in the ListView:

|年份 | BeginPoint |端点| BeginDescription |结束说明|条件1 | Condition2 |

|Year | BeginPoint | EndPoint | BeginDescription | EndDescription | Condition1 | Condition2 |

数据可能看起来像这样(映射到以上各列):

2006、0.0、0.9,"BEG","END" ;,“ N”,“ M”

2008、0.0、0.9,"BEG","END" ;,“ N”,“ S”

2006, 0.9, 1.4, “TEST”, “ENDER”, “N”, “N”

2008, 0.9, 1.4, “TEST”, “ENDER”, “N”, “N”

So, I want to display the data for the years of data collected for each Section. So, lets assume that a Section with Id 1, could have SectionData for 2006 and 2008, and a Section with Id 2 has SectionData for 2006. How do I use WPF databinding in a ListView to display this collection of Sections with their collection of SectionData?

So, I want to display the data for the years of data collected for each Section. So, lets assume that a Section with Id 1, could have SectionData for 2006 and 2008, and a Section with Id 2 has SectionData for 2006. How do I use WPF databinding in a ListView to display this collection of Sections with their collection of SectionData?

I will make a sample project and post code if it would be helpful. Thanks for any suggestions!

I will make a sample project and post code if it would be helpful. Thanks for any suggestions!

推荐答案

http://karlshifflett.wordpress.com/2009/06/10/wpf-sample-series-listbox-grouping-sorting-subtotals-and-collapsible-regions/


这篇关于WPF ListView数据绑定-具有实体集合的实体集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 10:33