2009-12-18 53 views
3

在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;,但它並不在我的事件處理程序

回答

8

列表工作沒有行爲,通知項目計數被改變。您應該使用支持列表INotifyCollectionChanged ..例如:ObservableCollection <T>ObservableCollection <T>會通知您的lvItems項目數已更改,並且會正確顯示。

+0

我想我不需要INotifyCollectionChanged是因爲我通過WCF服務刷新List進行循環,但無論如何感謝您的幫助,您讓我以正確的方式。我使用中間ObservableCollection,現在一切正常。我把我的解決方案作爲可讀性的答案。謝謝。 +1 – rem 2009-12-18 15:00:35

+2

ObservableCollection繼承自INotifyCollectionChanged :) – RredCat 2011-06-14 11:00:53

5

使用中間的ObservableCollection:

ObservableCollection<Meets> _MeetCollection = 
      new ObservableCollection<Meets>(); 

    public ObservableCollection<Meets> MeetCollection 
    { get { return _MeetCollection; } } 


    public class Meets 
    { 
     public string Date { get; set; } 
     public string Time { get; set; } 
     public string Description { get; set; } 

    } 

在事件處理循環中,我們提出:

 private void AllMeetings() 
    { 
     Customer_ServiceClient service = new Customer_ServiceClient(); 
     _MeetCollection.Clear(); 
     foreach (MeetList meet in service.ReadMeetList()) 
     { 
      _MeetCollection.Add(new Meets 
      { 
       Date = meet.MeetDate, 
       Time = meet.MeetTime, 
       Description = meet.MeetDescr 
      });        
     }    
    } 

並以XAML綁定改爲:

<Grid> 
    <ListView Height="100" Width="434" Margin="0,22,0,0" Name="lvItems" ItemsSource="{Binding MeetCollection}"> 
     <ListView.View> 
     <GridView> 
      <GridViewColumn Header="Date" Width="100" DisplayMemberBinding="{Binding Date}"/> 
      <GridViewColumn Header="Time" Width="100" DisplayMemberBinding="{Binding Time}"/> 
      <GridViewColumn Header="Description" Width="200" DisplayMemberBinding="{Binding Description}"/> 
     </GridView> 
     </ListView.View> 
    </ListView> 
    </Grid>