2012-01-12 3 views
0

저는 WCF를 사용하여 실버 라이트에서 콤보 박스를 바인딩하려고합니다. 내가 코드를 아래에 시도했지만 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> 

없음 값이 채워집니다 Heres는 내 메인 페이지 코드

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

두 개의 목록이있는 개체를 반환합니다. 클라이언트는 표시 방법을 결정할 수 없습니다. 구조체를 목록의 항목/배열로 변경하는 것이 좋습니다.

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를 사용하여 combobox를 바인딩하는 일반적인 방법은 무엇입니까 (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; 
    } 

희망이 있습니다.

관련 문제