WPF Listview binding to a generic list dynamically populated by WCF

In a WPF application, I have a WCF service that dynamically populates a shared Listobject from a database.

How in this case ( Listcreated at runtime), could I bind items Listto ListView objects?

This is the data contract for my web service:

....
[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
    }

Here in this event handler, I read the WCF and Loop service through a List object:

       private void AllMeetings()
    {
        Customer_ServiceClient service = new Customer_ServiceClient();
        foreach (MeetList meet in service.ReadMeetList())
        {
            ?????? = meet.MeetDate; // it here that I bumped into a problem
            ?????? = meet.MeetTime; //
            ?????? = meet.MeetDescr;//
        }

    }

My 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>

And the data templates for this ListView are:

<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>
......

As in this case ( Listcreated at runtime), I could bind my common Listitems to ListView objects?

I tried to use lvItems.ItemsSource = profiles;, but it does not work in the event handler

+3
2

, , . INotifyCollectionChanged.. : ObservableCollection <T> . ObservableCollection <T> lvItems , , .

+8

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>
+5

Source: https://habr.com/ru/post/1726040/


All Articles