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