在WPF應用程序中,我有一個WCF服務,它動態地從後端數據庫中填充一個通用的List
對象。WPF Listview數據綁定到通用列表,由WCF服務動態填充
在這種情況下(List
在運行時創建),我可以將List
項綁定到ListView對象項目?
這是我的Web服務的數據合同:
private void AllMeetings()
{
Customer_ServiceClient service = new Customer_ServiceClient();
foreach (MeetList meet in service.ReadMeetList())
{
?????? = meet.MeetDate; // it's here that I bumped into a problem
?????? = meet.MeetTime; //
?????? = meet.MeetDescr;//
}
}
我的列表視圖XAML:
....
[DataContract]
public class MeetList
{
[DataMember]
public string MeetDate;
[DataMember]
public string MeetTime;
[DataMember]
public string MeetDescr;
.....
static internal List<MeetList> LoadMeetings(string dynamicsNavXml)
{
...// Loads XML stream into the WCF type
}
在此事件處理我讀通過List對象的WCF服務,並循環
這裏
<Grid>
<ListView Height="100" Width="434" Margin="0,22,0,0" Name="lvItems" ItemsSource="{Binding}" IsSynchronizedWithCurrentItem="True" SelectionMode="Single">
<ListView.View>
<GridView>
<GridViewColumn Header="Date" Width="100" HeaderTemplate="{StaticResource DateHeader}" CellTemplate="{DynamicResource DateCell}"/>
<GridViewColumn Header="Time" Width="100" HeaderTemplate="{StaticResource TimeHeader}" CellTemplate="{DynamicResource TimeCell}"/>
<GridViewColumn Header="Description" Width="200" HeaderTemplate="{StaticResource DescriptionHeader}" CellTemplate="{DynamicResource DescriptionCell}"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
此ListView的數據模板:
<Window.Resources>
<DataTemplate x:Key="DateHeader">
<StackPanel Orientation="Horizontal">
<TextBlock Margin="10,0,0,0" Text="Date" VerticalAlignment="Center" />
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="DateCell" DataType="Profile">
<StackPanel Orientation="Horizontal">
<TextBlock>
<TextBlock.Text>
<Binding Path="MeetDate" />
</TextBlock.Text>
</TextBlock>
</StackPanel>
</DataTemplate>
......
如何在這種情況下(在運行時創建的List
),我可以我一般List
項目綁定到ListView對象的項目?
我試圖用lvItems.ItemsSource = profiles;
,但它並不在我的事件處理程序
我想我不需要INotifyCollectionChanged是因爲我通過WCF服務刷新List進行循環,但無論如何感謝您的幫助,您讓我以正確的方式。我使用中間ObservableCollection,現在一切正常。我把我的解決方案作爲可讀性的答案。謝謝。 +1 – rem 2009-12-18 15:00:35
ObservableCollection繼承自INotifyCollectionChanged :) – RredCat 2011-06-14 11:00:53