C# 使用wcf在silverlight中绑定组合框时出现问题
我正在尝试使用WCF在silverlight中绑定combobox。我已经尝试了下面的代码,但combobox不显示任何值??。。代码如下C# 使用wcf在silverlight中绑定组合框时出现问题,c#,silverlight,wcf,silverlight-4.0,C#,Silverlight,Wcf,Silverlight 4.0,我正在尝试使用WCF在silverlight中绑定combobox。我已经尝试了下面的代码,但combobox不显示任何值??。。代码如下 public class Appsrvvice : IAppsrvvice { public void DoWork() { } public List<fillupcombox> fillup() { List<string> x=new List<string
public class Appsrvvice : IAppsrvvice
{
public void DoWork()
{
}
public List<fillupcombox> fillup()
{
List<string> x=new List<string>();
List<string> y=new List<string>();
string connectionstring = "server=localhost;User Id=root;password=root;Persist Security Info=True;database=mv_store";
string msg;
msg = "";
MySqlConnection con = new MySqlConnection(connectionstring);
MySqlDataAdapter ad = new MySqlDataAdapter("select Product_Name,Product_Id from product_detail Order by Product_Name", con);
DataTable dt = new DataTable();
try
{
ad.Fill(dt);
// return dt;
for(int i=0;i<dt.Rows.Count;i++)
{
x.Add(dt.Rows[i]["Product_Name"].ToString());
y.Add(dt.Rows[i]["Product_Id"].ToString());
}
}
catch (Exception e)
{
msg = e.Message;
return null;
}
finally
{
ad.Dispose();
}
return new List<fillupcombox>()
{
new fillupcombox()
{
Texts=x,
Valuess=y
}
};
}
}
[ServiceContract]
public interface IAppsrvvice
{
[OperationContract]
void DoWork();
[OperationContract]
List<fillupcombox> fillup();
}
[DataContract]
public class fillupcombox
{
[DataMember]
public List<string> Texts
{
get;
set;
}
[DataMember]
public List<string> Valuess
{
get;
set;
}
}
公共类Appsrvvice:IAppsrvvice
{
公共工作
{
}
公共列表填充()
{
列表x=新列表();
列表y=新列表();
string connectionstring=“服务器=localhost;用户Id=root;密码=root;持久安全信息=True;数据库=mv_存储”;
串味精;
msg=“”;
MySqlConnection con=新的MySqlConnection(connectionstring);
MySqlDataAdapter ad=新的MySqlDataAdapter(“按产品名称从产品详细订单中选择产品名称、产品Id”,con);
DataTable dt=新的DataTable();
尝试
{
ad.Fill(dt);
//返回dt;
对于(int i=0;i您正在返回一个包含2个列表的对象。客户端无法确定如何显示它们。我建议您将结构更改为项目列表/数组,例如:
class Pair
{
public string Key;
public string Value;
}
并从服务返回一个对数组
然后将DataTemplate添加到ComboBox,以便它知道如何显示一对:
<ComboBox ...>
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Key}"/>
<TextBlock Text="{Binding Value}"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
我也遇到了同样的问题,所以我决定尽可能让它变得简单
public List<string> GetTypes()
{
NissanDBEntities niss = new NissanDBEntities();
return niss.cars.Select(m => m.type).Distinct().ToList();
}
public Clients()
{
InitializeComponent();
NissanSvc.NissanServiceClient proxy = new Nissan.NissanSvc.NissanServiceClient();
proxy.GetTypesCompleted += new EventHandler<NissanSvc.GetTypesCompletedEventArgs (proxy_GetTypesCompleted);
proxy.GetTypesAsync();
}
void proxy_GetTypesCompleted(object sender, NissanSvc.GetTypesCompletedEventArgs e)
{
this.cmbType.ItemsSource = e.Result;
}
public List GetTypes()
{
NissandEntities niss=新的NissandEntities();
return niss.cars.Select(m=>m.type).Distinct().ToList();
}
公众客户()
{
初始化组件();
NissanSvc.NissanServiceClient proxy=新的Nissan.NissanSvc.NissanServiceClient();
proxy.GetTypesCompleted+=new EventHandler他在他的fillupCompletedp回调中有这个。你的xaml是什么样子的?我已经发布了我的xaml。请检查这不是他们除了添加模板以外的其他方法吗?@pheonix4eva-你想要什么?使用wcf绑定combobox的传统方法是什么。(不打算使用LINQ).?正是我写的。这是传统的方式。
<ComboBox ...>
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Key}"/>
<TextBlock Text="{Binding Value}"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
public List<string> GetTypes()
{
NissanDBEntities niss = new NissanDBEntities();
return niss.cars.Select(m => m.type).Distinct().ToList();
}
public Clients()
{
InitializeComponent();
NissanSvc.NissanServiceClient proxy = new Nissan.NissanSvc.NissanServiceClient();
proxy.GetTypesCompleted += new EventHandler<NissanSvc.GetTypesCompletedEventArgs (proxy_GetTypesCompleted);
proxy.GetTypesAsync();
}
void proxy_GetTypesCompleted(object sender, NissanSvc.GetTypesCompletedEventArgs e)
{
this.cmbType.ItemsSource = e.Result;
}