2011-02-14 94 views
0

我在網格外部有一個Silverlight組合框,可以正常工作。 但是,我無法讓它在數據網格內正常工作。我不確定我做錯了什麼。非常感謝幫助! 此代碼工作正常的Silverlight的組合框格之外:DataGrid中的Silverlight組合框

XAML:

<ComboBox Height="23" HorizontalAlignment="Left" ItemsSource="{Binding ElementName=comboBoxItemDomainDataSource, Path=Data}" Margin="112,72,0,0" Name="comboBoxItemComboBox" VerticalAlignment="Top" Width="185" SelectionChanged="comboBoxItemComboBox_SelectionChanged" DisplayMemberPath="ComboDisplayValue"> 
      <ComboBox.ItemsPanel> 
       <ItemsPanelTemplate> 
        <VirtualizingStackPanel /> 
       </ItemsPanelTemplate> 
      </ComboBox.ItemsPanel> 
     </ComboBox> 
    </Grid> 
    <riaControls:DomainDataSource AutoLoad="True" d:DesignData="{d:DesignInstance my:ComboBoxItem, CreateList=true}" Height="0" LoadedData="comboBoxItemDomainDataSource_LoadedData" Name="comboBoxItemDomainDataSource" QueryName="GetComboboxItems_PatIdEssentrisQuery" Width="0"> 
     <riaControls:DomainDataSource.DomainContext> 
      <my:ComboBoxItemContext /> 
     </riaControls:DomainDataSource.DomainContext> 
    </riaControls:DomainDataSource> 


Combo Box Class: 
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.ComponentModel.DataAnnotations; 
using System.Data; 
using System.Data.SqlClient; 
using System.Configuration; 

namespace CorporateHR.Web 
{ 
    public class ComboBoxItem 
    { 
     [Key] 
     public int ComboID_Int { get; set; } 
     public string ComboDisplayValue { get; set; } 

     private static List<ComboBoxItem> GetComboBoxItems(string strStoredProcedure) 
     { 
      SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["RefConnectionString"].ConnectionString); 
      SqlCommand cmd = new SqlCommand(strStoredProcedure, con); 
      cmd.CommandType = System.Data.CommandType.StoredProcedure; 

      List<ComboBoxItem> comboList = new List<ComboBoxItem>(); 
      con.Open(); 
      SqlDataReader dr = cmd.ExecuteReader(behavior: CommandBehavior.CloseConnection); 

      while (dr.Read()) 
      { 
       ComboBoxItem ComboBoxItem = new ComboBoxItem(); 
       ComboBoxItem.ComboID_Int = Convert.ToInt32(dr[0].ToString()); 
       ComboBoxItem.ComboDisplayValue = dr[1].ToString(); 
       comboList.Add(ComboBoxItem); 
      } 
      return comboList; 


     } 


     public static List<ComboBoxItem> GetComboboxItems_PatIdEssentris() 
     { 
      return GetComboBoxItems("uspLookupPatIdEssentris"); 
     } 

     //Secondary ComboBox Lookup: 
     public static List<ComboBoxItem> GetComboboxItems_ORStatus() 
     { 
      return GetComboBoxItems("uspLookupORStatus"); 
     } 


    } 
} 

組合框域服務:

namespace CorporateHR.Web 
{ 
    using System; 
    using System.Collections.Generic; 
    using System.ComponentModel; 
    using System.ComponentModel.DataAnnotations; 
    using System.Linq; 
    using System.ServiceModel.DomainServices.Hosting; 
    using System.ServiceModel.DomainServices.Server; 


    // TODO: Create methods containing your application logic. 
    [EnableClientAccess()] 
    public class ComboBoxItemService : DomainService 
    { 
     public IEnumerable<ComboBoxItem> GetComboboxItems_PatIdEssentris() 
     { 
      return ComboBoxItem.GetComboboxItems_PatIdEssentris(); 
     } 
     public IEnumerable<ComboBoxItem> GetComboboxItems_ORStatus() 
     { 
      return ComboBoxItem.GetComboboxItems_ORStatus(); 
     } 
    } 

} 

代碼頁背後(用於組合框其中填充) :

private void comboBoxItemDomainDataSource_LoadedData(object sender, LoadedDataEventArgs e) 
     { 

      if (e.HasError) 
      { 
       System.Windows.MessageBox.Show(e.Error.ToString(), "Load Error", System.Windows.MessageBoxButton.OK); 
       e.MarkErrorAsHandled(); 
      } 
     } 

     private void comboBoxItemComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) 
     { 

     } 
+0

什麼是問題,DataGrid在哪裏? – vorrtex 2011-02-14 22:28:31

回答

0

我使用瞭如下模板化列:

<sdk:DataGridTemplateColumn.CellTemplate> 
    <DataTemplate> 
     <TextBlock Margin="2" VerticalAlignment="Center" HorizontalAlignment="Left" 
        Text="{Binding Path=Option0, Mode=OneWay}" Width="Auto" /> 
    </DataTemplate> 
</sdk:DataGridTemplateColumn.CellTemplate> 
<sdk:DataGridTemplateColumn.CellEditingTemplate> 
    <DataTemplate> 
     <StackPanel Orientation="Horizontal"> 
      <ComboBox Height="23" Name="cbx0" SelectedValuePath="Display" DisplayMemberPath="Display" 
       SelectedValue="{Binding Path=Option0, Mode=TwoWay}" 
       ItemsSource="{Binding Source={StaticResource DataContextProxy},Path=DataSource.ocList0}" 
         MinWidth="65" 
       Width="Auto"> 

      </ComboBox> 
     </StackPanel> 
    </DataTemplate> 
</sdk:DataGridTemplateColumn.CellEditingTemplate> 

哪裏_ocList0是ObservableCollection<cComboBoxOption> 的類型,這是cComboBoxOption類:

public class cComboBoxOption 
{ 
    public int Id { get; set; } 
    public string Display { get; set; } 

    public cComboBoxOption(int id, string name) 
    { 
     this.Id = id; 
     this.Display = name; 

    } 

} 

這是寫在一個通用的方法,因爲我不知道該綁定將是什麼,或者組合框將包含,直到運行時間。

更簡單的方法是使用List<string>查看博客文章HERE

enter image description here