/// <summary>
        /// 单选模式下的写值操作
        /// </summary>
        /// <param name="curEntity">正在编辑的实体。</param>
        /// <param name="svm">选择视图模型</param>
        /// <param name="selectedEntities">当前被选择的实体</param>
        private SetSelectionResult SyncSelectionToValue_Single(Entity curEntity, SelectionViewMeta svm, IList<Entity> selectedEntities)
        {
            bool success = false;

            //引用属性,应该先尝试设置实体属性,再设置 Id 属性。
            var rp = this.Meta.PropertyMeta.ManagedProperty as IRefProperty;
            if (selectedEntities.Count > 0)
            {
                var selectedEntity = selectedEntities[0] as Entity;
                if (rp != null)
                {
                    //如果 SelectedValuePath 是一个引用属性,或者直接就是一个实体属性,
                    //则应该获取相应的实体的值。
                    var valuePath = svm.SelectedValuePath;
                    if (valuePath != null)
                    {
                        if (valuePath is IRefProperty)
                        {
                            selectedEntity = selectedEntity.GetRefEntity((valuePath as IRefProperty).RefEntityProperty);
                        }
                        else if (rp.RefEntityType.IsAssignableFrom(valuePath.PropertyType))
                        {
                            selectedEntity = this.GetSelectedValue(selectedEntity) as Entity;
                        }
                    }

                    //设置实体到本引用属性上。
                    this.OnReferenceEntityChanging();
                    curEntity.SetRefEntity(rp.RefEntityProperty, selectedEntity, ManagedPropertyChangedSource.FromUIOperating);
                    success = curEntity.GetRefEntity(rp.RefEntityProperty) == selectedEntity;
                    if (success) { this.OnReferenceEntityChanged(); }
                }
                else
                {
                    var value = this.GetSelectedValue(selectedEntity);
                    this.PropertyValue = value;
                    success = this.PropertyValue == value;
                    if (success && svm.RefIdHost != null)
                    {
                        curEntity.SetProperty(svm.RefIdHost, selectedEntity.Id, ManagedPropertyChangedSource.FromUIOperating);
                    }
                }
            }
            else
            {
                if (rp != null)
                {
                    this.OnReferenceEntityChanging();
                    curEntity.SetRefEntity(rp.RefEntityProperty, null, ManagedPropertyChangedSource.FromUIOperating);
                    success = curEntity.GetRefEntity(rp.RefEntityProperty) == null;
                    if (success) { this.OnReferenceEntityChanged(); }
                }
                else
                {
                    this.PropertyValue = null;
                    success = this.PropertyValue == null;
                    if (success && svm.RefIdHost != null)
                    {
                        curEntity.SetProperty(svm.RefIdHost, null, ManagedPropertyChangedSource.FromUIOperating);
                    }
                }
            }

            return success ? SetSelectionResult.Success : SetSelectionResult.Cancel;
        }
Example #2
0
        /// <summary>
        /// 使用这个查询面板中的查询对象数据,
        /// 给实体的外键设置值。
        /// </summary>
        /// <param name="entity"></param>
        internal void SyncRefEntities(Entity entity)
        {
            if (entity == null) throw new ArgumentNullException("newEntity");

            var criteria = this.Current;
            if (criteria != null)
            {
                var destProperties = entity.PropertiesContainer.GetAvailableProperties();

                //对每一个导航的实体引用属性,都给 referenceEntity 赋相应的值
                //只有导航查询实体中的引用实体ID属性名和被查询实体的引用实体ID属性名相同时,才能设置
                foreach (var naviProperty in this.Meta.EntityProperties)
                {
                    //如果是一个引用实体属性
                    var criteriaRefId = naviProperty.PropertyMeta.ManagedProperty as IRefIdProperty;
                    if (criteriaRefId != null)
                    {
                        foreach (var mp in destProperties)
                        {
                            var entityRef = mp as IRefEntityProperty;

                            //约定:被查询实体的引用实体ID属性名与 naviProperty 的名称相同,并且二者类型一致时,才能被设置。
                            if (entityRef != null
                                && entityRef.RefIdProperty.Name == criteriaRefId.Name
                                && criteriaRefId.RefEntityType == entityRef.RefEntityType
                                )
                            {
                                //读值,并写值到新对象中。
                                var value = criteria.GetRefEntity(criteriaRefId.RefEntityProperty);
                                entity.SetRefEntity(entityRef, value);
                                break;
                            }
                        }
                    }
                }
            }
        }