【发布时间】:2012-05-21 18:05:03
【问题描述】:
我正在尝试使用 MVVM 将从 WCF 服务返回的数据绑定到 WPF 中的网格。当我在视图模型中使用 WCF 服务的逻辑时也是如此。
代码隐藏:
this.DataContext = new SampleViewModel();
查看/XAML:
<Window x:Class="Sample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<DataGrid ItemsSource="{Binding Students}" AutoGenerateColumns="False" >
<DataGrid.Columns>
<DataGridTextColumn Header="ID" Binding="{Binding ID}" />
<DataGridTextColumn Header="Name" Binding="{Binding Name}" />
<DataGridTextColumn Header="Address" Binding="{Binding Address}" />
</DataGrid.Columns>
</DataGrid>
</Grid>
</Window>
查看模型:
public List<Student> Students {
get {
var service = new StudentServiceClient();
var students = new List<Student>(service.GetStudents());
return students;
}
}
学生服务:
[ServiceContract]
public interface IStudentService {
[OperationContract]
IEnumerable<Student> GetStudents();
}
[DataContract]
public class Student {
public string Name { get; set; }
public int ID { get; set; }
public string Address { get; set; }
}
StudentService.svc:
public class StudentService : IStudentService {
public IEnumerable<Student> GetStudents() {
var students = new List<Student>();
for (int i = 0; i < 3; i++) {
students.Add(new Student {
Name = "Name" + i,
ID = i,
Address = "Address" + 1
});
}
return students;
}
}
当我运行应用程序时,我在网格中看不到蚂蚁记录..
【问题讨论】:
-
修复了问题.. 缺少数据合同中的 DataMember 属性
标签: wcf mvvm wpfdatagrid