我是WPF的初学者。我正在尝试使用从wcf服务
检索的可观察集合来填充组合框xaml代码:
<ComboBox x:Name="cmbclass" ItemsSource="{Binding ClassList}" VerticalAlignment="Top" DisplayMemberPath="ClassName" SelectedValuePath="ClassID"/>
消费服务:
ServiceReference1.Service1Client ClassList = new ServiceReference1.Service1Client();
ObservableCollection<ClassDO> Classlist = new ObservableCollection<ClassDO>();
Classlist = (ObservableCollection<ClassDO>)ClassList.GetClassList();
cmbclass.ItemsSource = Classlist;
cmbclass.DisplayMemberPath = "ClassID";
cmbclass.SelectedValuePath = "CLassName";
我得到以下错误,有人可以帮助我吗?
错误1
'SchoolManagementSystem.ServiceReference1.ClassDO []'到 System.Collections.ObjectModel.ObservableCollection'D:\ Dotnet \ Projects \ WPF \ WindowsPhoneApps \ SchoolManagementSystem \ SchoolManagementSystem \ MainWindow.xaml.cs 37 23 SchoolManagementSystemCannot转换类型
任何帮助都将不胜感激。
答案 0 :(得分:0)
请看一下这段代码。此示例使用MVVM design pattern
ViewModel
public class MainViewModel
{
public MainViewModel()
{
ServiceReference1.Service1Client client = new erviceReference1.Service1Client();
Classlist = new ObservableCollection<ClassDO>(client.GetClassList());
}
public ObservableCollection<ClassDO> ClassList { get; set; }
}
视图
<ComboBox ItemsSource="{Binding ClassList}" VerticalAlignment="Top" DisplayMemberPath="ClassName" SelectedValuePath="ClassID"/>
在视图的代码隐藏中设置datacontext
this.DataContext = new MainViewModel();
在这个例子中,我假设GetClassList()
IEnumerable ClassDO
返回
答案 1 :(得分:0)
我得到了如何用WCF消耗的可观察集合来填充组合框 Xaml代码:
<ComboBox x:Name="Cmbclass" ItemsSource="{Binding Classlist}" VerticalAlignment="Center" DisplayMemberPath="ClassName" SelectedValuePath="ClassID" Loaded="CmbClassListLoaded"></ComboBox>
Consumingservice:
public void GetClassList()
{
ServiceReference1.Service1Client ServiceClient = new ServiceReference1.Service1Client();
ObservableCollection<ClassDO> Classlist = ServiceClient.GetClassList();
Cmbclass.ItemsSource = Classlist;
Cmbclass.DisplayMemberPath = "classname";
//Cmbclass.se = "ClassID";
Cmbclass.SelectedValuePath = "classid";
}
现在在windowload方法中调用Getclasslist