WPF Listview数据绑定到通用List,由WCF服务动态填充

rem*_*rem 3 c# data-binding wpf wcf listview

在WPF应用程序中,我有一个WCF服务,它List从后端数据库动态填充通用 对象.

在这种情况下(List在运行时创建),我可以将List项绑定到ListView对象项?

它是我的Web服务的数据合同:

....
[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
    }
Run Code Online (Sandbox Code Playgroud)

在这个事件处理程序中,我读取了WCF服务并通过List对象循环:

       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;//
        }

    }
Run Code Online (Sandbox Code Playgroud)

我的Listview XAML:

                            <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>
Run Code Online (Sandbox Code Playgroud)

和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>
......
Run Code Online (Sandbox Code Playgroud)

在这种情况下(List在运行时创建),我可以将我的通用List项绑定到ListView对象项?

我尝试使用lvItems.ItemsSource = profiles;,但它在我的事件处理程序中不起作用

Rre*_*Cat 8

列表没有通知项目计数已更改的行为.您应该使用支持INotifyCollectionChanged的列表,例如:ObservableCollection <T>.ObservableCollection <T>将通知您的lvItems项目计数已更改并将正确显示.

  • ObservableCollection继承自INotifyCollectionChanged :) (3认同)

rem*_*rem 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; }

    }
Run Code Online (Sandbox Code Playgroud)

在事件处理程序循环中,我们放置:

       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
            });                               
        }            
    }
Run Code Online (Sandbox Code Playgroud)

并且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>
Run Code Online (Sandbox Code Playgroud)