在lightswitch应用程序中的AutoCompleteBox中设置默认值

时间:2014-09-01 15:10:30

标签: .net silverlight visual-studio-lightswitch

我有2个表(假设订单和商品)订单有对Items表的外键引用。 Items的ItemType类型为string,ItemType绑定到选择List,其中包含TypeA,typeB,TypeC ......

我为订单和项目创建了一个屏幕(列表详细信息屏幕),在RowLayout中呈现订单,并在行布局内的数据网格中呈现项目。项目类型绑定到自动完成框。

所有内容都按预期工作项目类型将从关联列表中填充。

现在我想设置AutoCompleteBox的默认值让我说TypeA每当我在数据网格中创建一个新行时,我尝试在Items_changed事件中设置ItemType属性,当action是Add(NotifyCollectionChangedAction.Add)并尝试了几个其他选项但没有成功。

我能够在Items_changed事件中设置简单属性的值,但无法设置ItemType。

有人可以告诉我如何实现这一目标吗?

1 个答案:

答案 0 :(得分:0)

相对简单。在Items实体的_Created()方法中,添加一行代码以指定默认类型值。我更喜欢将查找表用于实体本地枚举,所以我通常会通过LINQ查询来执行此操作。假设以下权利集合:

  • 订单
  • 的OrderItems
  • OrderItemTypes

我对OrderItems上的Created()方法的代码隐藏看起来像这样。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.LightSwitch;
namespace LightSwitchApplication
{
    public partial class OrderItem
    {

        partial void OrderItem_Created()
        {
            this.Quantity = 0;
            if ((from OrderItemType oit in DataWorkspace.ApplicationData.OrderItemTypes
                 where oit.IsDefaultOrderItemType == true
                 select oit).Count() > 0)
            {
                this.OrderItemType = (from OrderItemType oit in DataWorkspace.ApplicationData.OrderItemTypes
                                      where oit.IsDefaultOrderItemType == true
                                      select oit).FirstOrDefault();
            }
        }
    }
}

请注意,查询使用OrderItemType实体上的IsDefaultOrderItemType字段来确定要将哪个OrderItemTypes条目用作默认值。如果您愿意,也可以使用OrderItemType.TypeName或您用于OrderItemType的任何描述符作为查询中的选择器。

希望有所帮助。 :)

PS:除非你想覆盖数据输入,否则不要在_Changed()处理程序中以编程方式设置值!