WPF ListView数据绑定-具有实体集合的实体集合 [英] WPF ListView Databinding - Collection of entities with a collection of entities
问题描述
首先,这是我的第一个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.
我正在使用MVVM编写WPF应用程序,该应用程序使用WCF服务从Oracle数据库中获取数据并以ObservableCollection的形式返回数据.我的实体类. 在这篇文章中,为简单起见,我在这里更改了名称.
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.
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:
此处是Section实体集合的属性.
Here’s the property for the Collection of Section entities. Each Section contiains an ObservableCollection of SectionData.
public ObservableCollection < Section > SectionCollection
public ObservableCollection<Section> SectionCollection
{
获取 {返回 _sectionCollection; }
get { return _sectionCollection; }
设置
set
{
如果(_sectionCollection == 值)
if (_sectionCollection == value)
返回;
return;
值;
_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:
< x : 类 =" WpfApplicationTest.SectionView" =" http://schemas .microsoft.com/winfx/2006/xaml/presentation;
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
: x =" http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
: mc =" http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
: d =" http://schemas.microsoft.com/expression/blend/2008"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
: 模型 =" clr-namespace:Entities; assembly = Entities"
xmlns:model="clr-namespace:Entities;assembly=Entities"
: 不可思议 =" d"
mc:Ignorable="d"
: DesignHeight ="196" d : DesignWidth ="548">
d:DesignHeight="196" d:DesignWidth="548">
< 名称 =" listViewSectionData" ItemsSource SectionCollection }>
<ListView Name="listViewSectionData" ItemsSource="{Binding SectionCollection}">
< ListView.View
< >
<GridView>
<!-- I know that this isn't the way to display the year, but don't know how to do it. -->
< 页眉 DisplayMemberBinding SectionDataCollection .SurveyYear}". />
<GridViewColumn Header=" Year " DisplayMemberBinding="{Binding SectionDataCollection.SurveyYear}" />
< 页眉 DisplayMemberBinding BeginPoint }"//>
<GridViewColumn Header=" Begin Point " DisplayMemberBinding="{Binding BeginPoint}"/>
< GridViewColumn ="端点 DisplayMemberBinding EndPoint }"/>
<GridViewColumn Header=" End Point " DisplayMemberBinding="{Binding EndPoint}"/>
< 页眉 DisplayMemberBinding BeginDescription }"//>
<GridViewColumn Header=" Begin Description " DisplayMemberBinding="{Binding BeginDescription}"/>
< 页眉 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>
</ >
</ListView.View>
</ >
</ >
MainWindow.xaml:
< x : 类 =" WpfApplicationTreeViewTest.MainWindow" =" http://schemas .microsoft.com/winfx/2006/xaml/presentation;
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
: x =" http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
: 本地 =" clr-namespace:WpfApplicationTest"
xmlns:local="clr-namespace:WpfApplicationTest"
: 视图模型 =" clr-namespace:WpfApplicationTest.ViewModel"
xmlns:viewmodel="clr-namespace:WpfApplicationTest.ViewModel"
="MainWindow" 高度 ="472" 宽度 ="616"
Title="MainWindow" Height="472" Width="616"
="宽度">
SizeToContent="Width">
< >
<!-将SectionView绑定到MainWindow构造函数中定义的SectionViewModel. ->
<!-- Bind a SectionView to the SectionViewModel defined in the MainWindow constructor. -->
< DataType x : viewmodel : }">
<DataTemplate DataType="{x:Type viewmodel:SectionViewModel}">
< : />
<local:SectionView />
</ >
</DataTemplate>
</ > 跨度>
< >
< ContentControl 内容 =&; { }" />
<ContentControl Content="{Binding}" />
</ >
</ >
MainWindow.xaml.cs:
public {
InitializeComponent();
此 .DataContext = 新 SectionViewModel (新 第(1,0.0,0.9, " BEGIN" ," END” )));
this.DataContext = new SectionViewModel(new Section(1, 0.0, 0.9, "BEGIN", "END”));
}
那不是我想要的.
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!
推荐答案
What I understand is you are looking for a ListView with group. If so the following link has good example
What I understand is you are looking for a ListView with group. If so the following link has good example
这篇关于WPF ListView数据绑定-具有实体集合的实体集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!