我在网格外面有一个Silverlight组合框,工作正常。 但是,我无法让它在数据网格内正常工作。我不确定我做错了什么。非常感谢帮助! 此代码适用于网格外的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 :(得分: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。