我正在尝试使用WCF在silverlight中绑定组合框。我试过下面的代码,但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我的主页代码
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>
组合框中没有填充值。知道我哪里错了吗?
答案 0 :(得分:1)
comboBox1.ItemsSource = e.Result;
答案 1 :(得分:1)
我遇到了同样的问题,所以我决定把它作为基本的可行。
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;
}
希望它有所帮助。
答案 2 :(得分:0)
您正在返回一个包含2个列表的对象。客户端无法确定如何显示它们。我建议您将结构更改为项目列表/数组,例如:
class Pair
{
public string Key;
public string Value;
}
从服务中返回一组Pairs。
然后将一个DataTemplate添加到ComboBox,以便它知道如何显示一对:
<ComboBox ...>
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Key}"/>
<TextBlock Text="{Binding Value}"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>