我想從WCF服務返回的數據綁定到使用MVVM的WPF中的網格。當我在視圖模型中使用WCF服務的邏輯時也是如此。使用MVVM模式綁定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;
}
}
IStudentService:
[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屬性 – Arihant