当我点击组合框中的项目( SelectionChanged 事件)时,我需要将ComboBox
中的项目插入我的Datagrid
。
我使用以下方法将数据从WCF应用程序加载到组合框中:
private async Task LoadItems(TruckServiceClient TSC, QuoteOptionType type, ComboBox combobox)
{
List<DisplayItems> displayItems = new List<DisplayItems>();
foreach (var item in await TSC.GetQuoteOptionListAsync(type))
displayItems.Add(new DisplayItems { Id = item.Key, Name = item.Value });
combobox.ItemsSource = (displayItems.ToArray());
}
然后我将哪些数据排在我的 WindowLoaded 事件的哪个位置:
private async void QuoteWindow_Loaded(object sender, RoutedEventArgs e)
{
using (TruckServiceClient TSC = new TruckServiceClient())
{
await LoadItems(TSC, QuoteOptionType.BodyType, cmbBodyType);
await LoadItems(TSC, QuoteOptionType.Chassis, cmbChassisCab);
await LoadItems(TSC, QuoteOptionType.PaintColor, cmbPaint);
await LoadItems(TSC, QuoteOptionType.DropSide, cmbDropsideHeight);
await LoadItems(TSC, QuoteOptionType.Floor, cmbFloor);
await LoadItems(TSC, QuoteOptionType.RearDropSide, cmbRearDropsideHeight);
await LoadItems(TSC, QuoteOptionType.Extras, cmbAddExtras);
}
}
我非常想知道的是,我将如何显示我刚刚点击的项目(在组合框中)以添加到数据网格中?
我已经尝试过我在互联网上找到的以下编码,但我甚至不知道从哪里开始将编码从下面的内容更改为我需要如何使用当前的方式将项目加载到组合框。
Extras ex = (Extras)cmbAddExtras.SelectedItem;
List<Extra> items = new List<Extra> { E };
items.Where(item => item != null).ToList().ForEach(i =>
{
dgAddExtras.Items.Add(i);
});
答案 0 :(得分:2)
在XAML或Loaded方法上订阅SelectiongChanged
事件。然后在事件处理程序中只需检索所选项并将其添加到DataGrid:
private async void QuoteWindow_Loaded(object sender, RoutedEventArgs e)
{
using (TruckServiceClient TSC = new TruckServiceClient())
{
await LoadItems(TSC, QuoteOptionType.BodyType, cmbBodyType);
await LoadItems(TSC, QuoteOptionType.Chassis, cmbChassisCab);
await LoadItems(TSC, QuoteOptionType.PaintColor, cmbPaint);
await LoadItems(TSC, QuoteOptionType.DropSide, cmbDropsideHeight);
await LoadItems(TSC, QuoteOptionType.Floor, cmbFloor);
await LoadItems(TSC, QuoteOptionType.RearDropSide, cmbRearDropsideHeight);
await LoadItems(TSC, QuoteOptionType.Extras, cmbAddExtras);
}
combAddExtras.SelectionChanged += cmbAddExtras_SelectionChanged;
}
private void cmbAddExtras_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var item = cmbAddExtras.SelectedItem;
if (item != null)
dgAddExtras.Items.Add(item);
}
编辑 - 如果您要从组合中删除该项目:
private void cmbAddExtras_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var item = cmbAddExtras.SelectedItem;
if (item != null)
{
dgAddExtras.Items.Add(item);
cmbAddExtras.Remove(item);
}
}
如果您不想从Combo中删除它,但想检查它是否已经在DataGrid中:
private void cmbAddExtras_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var item = cmbAddExtras.SelectedItem;
if (item != null && !dgAddExtras.Items.Contains(item))
dgAddExtras.Items.Add(item);
}