使用wcf在silverlight中绑定combobox时出现问题

本文关键字:问题 combobox 绑定 wcf silverlight 使用 | 更新日期: 2023-09-27 18:20:04

我正在尝试使用WCF在silverlight中绑定combobox。我试过下面的代码,但comobox没有显示任何值??。。代码如下。。

 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;
    }
}

这是我的主页代码

 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>

组合框中没有填充任何值。知道我哪里错了吗?

使用wcf在silverlight中绑定combobox时出现问题

comboBox1.ItemsSource = e.Result;

我也遇到了同样的问题,所以我决定尽可能地让它变得基本。

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个列表的对象。客户端无法确定如何显示它们。我建议您将结构更改为项目列表/数组,例如:

class Pair
{
    public string Key;
    public string Value;
}

并从服务返回一个Pairs数组。

然后在组合框中添加一个DataTemplate,这样它就知道如何显示一对:

<ComboBox ...>
    <ComboBox.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                 <TextBlock Text="{Binding Key}"/>
                 <TextBlock Text="{Binding Value}"/>
            </StackPanel>
        </DataTemplate>
    </ComboBox.ItemTemplate>
</ComboBox>