2012-01-12 9 views
0

私はWCFを使用してSilverlightでコンボボックスをバインドしようとしています。私は、コードの下にしようとしているが、comoboboxは、任意の値を表示しません?? ...コードは次のように..wcfを使用してsilverlightでバインディングコンボボックスに問題があります

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値が中に取り込まれている相続人は私のメインページのコード

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; 

    } 

} 

コンボボックス。私はどこが間違っているのか?

+0

xamlはどのように見えますか? –

+0

私はxamlを投稿しました。それをチェックしてください – pheonix4eva

答えて

1
comboBox1.ItemsSource = e.Result; 
+0

彼はfillupCompletedpコールバックでそれを持っています。 –

+0

*今*彼はそれを持っています、彼は編集を行う前にそこにいませんでした。 :-) – OmegaMan

0

2つのリストでオブジェクトを返しています。クライアントは表示方法を決定できません。構造体をアイテムのリスト/配列に変更することをお勧めします。

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

サービスからペアの配列を返します。それがペアを表示する方法を知っているので、

そして、コンボボックスにDataTemplateを追加します。

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

テンプレートを追加する以外の方法はありませんか? – pheonix4eva

+0

@ pheonix4eva - 何が欲しいですか? –

+0

wcfを使用してコンボボックスをバインドする従来の方法は何ですか(LINQを使用する予定はありません)。 – pheonix4eva

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

希望します。

関連する問題