c# - WPF Listview 数据绑定(bind)到通用列表,由 WCF 服务动态填充

标签 c# wpf wcf data-binding listview

在 WPF 应用程序中,我有一个 WCF 服务,该服务动态填充来自后端数据库的通用 List 对象。

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

这是我的 Web 服务的数据契约(Contract):

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

在此事件处理程序中,我读取了 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;//
        }

    }

我的 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>

以及此 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 项绑定(bind)到 ListView 对象项?

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

最佳答案

List 没有通知项目计数已更改的行为。您应该使用支持 INotifyCollectionChanged 的列表。例如:ObservableCollectionObservableCollection 将通知您的 lvItems 项目计数已更改并且它将正确显示。

关于c# - WPF Listview 数据绑定(bind)到通用列表,由 WCF 服务动态填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1927139/

相关文章:

wpf - 带百分比坐标的面板

C#WPF适合应用程序取决于显示器大小在显示器之间移动后最大化/最小化后

c# - 在多线程中使用 WebBrowser 时出现访问冲突

c# - 为什么我的 soap header 中的证书有签名

c# - 检测页面是否在 iframe 中 - 服务器端

wpf - 为什么虚拟机中的 WPF 应用程序比直接在操作系统中运行的应用程序性能更好?

wcf - WCF 中的 ABC 代表什么?

c# - 使用 PropertyDescriptor 确定属性是否为复杂对象

.net - WCF 超时是一场噩梦

c# - Visual C# 中的全局变量