[英]Have issue in binding combobox in silverlight using wcf
i am trying to bind combobox in silverlight using WCF. 我试图使用WCF绑定silverlight中的组合框。 i have tried below code but comobobox doesn't display any values??.. code as follows..
我试过下面的代码,但comobobox没有显示任何值??代码如下..
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;
}
}
Heres My main page code 继承人我的主页代码
public MainPage()
{
InitializeComponent();
ServiceReference1.AppsrvviceClient obj = new ServiceReference1.AppsrvviceClient();
obj.fillupCompleted += new EventHandler<ServiceReference1.fillupCompletedEventArgs>(fillupCompletedp);
obj.fillupAsync();
}
public void fillupCompletedp(object sender, ServiceReference1.fillupCompletedEventArgs e)
{
comboBox1.ItemsSource =e.Result;
}
<UserControl x:Class="SilverlightApplication1comobobox.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400">
<Grid x:Name="LayoutRoot" Background="White">
<ComboBox Height="23" HorizontalAlignment="Left" Margin="70,67,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120" />
</Grid>
No Values is populated in the combobox. 组合框中没有填充值。 Any idea where i am wrong?
知道我哪里错了吗?
I had the same problem, So I decided to make it as basic as posible. 我遇到了同样的问题,所以我决定把它作为基本的,尽可能的。
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;
}
Hope it helps. 希望能帮助到你。
comboBox1.ItemsSource = e.Result;
You are returning an object with 2 Lists. 您将返回一个包含2个列表的对象。 the client is not able to determine how to display them.
客户端无法确定如何显示它们。 I suggest you change the structure into a List/Array of items, say:
我建议你把结构改成List / Array的项目,比如说:
class Pair
{
public string Key;
public string Value;
}
And return an array of Pairs from the service. 并从服务中返回一组Pairs。
Then add a DataTemplate to the ComboBox so it knows how to display a Pair: 然后将一个DataTemplate添加到ComboBox,以便它知道如何显示一对:
<ComboBox ...>
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Key}"/>
<TextBlock Text="{Binding Value}"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.