示例#1
0
        private static DataPropertyBinding GetDataPropertyBinding(JsonSingleModel model)
        {
            if (!dataBindingCache.TryGetValue(model, out DataPropertyBinding binding))
            {
                binding = new DataPropertyBinding(model);
                dataBindingCache[model] = binding;
            }

            return(binding);
        }
示例#2
0
        /// <summary>
        /// Datastore model binding to FrameworkElement default property
        /// </summary>
        /// <param name="element"></param>
        /// <param name="datastore"></param>
        /// <param name="path"></param>
        public static void SetDataBinding(this FrameworkElement element, IDatastore datastore, string path)
        {
            if (element is IJsonView jsonView)
            {
                string              propertyName = path.Split('/').Last();
                DependencyProperty  property     = jsonView.GetDefaultBindableProperty();
                DataPropertyBinding binding      = null;

                if (property == null)
                {
                    // Not Found 'DefaultBindingPropertyAttribute' or 'DependencyProperty' from Attribute PropertyName
                    return;
                }

                // Spectate
                if (path.Contains("{user}") ||
                    path.Contains("{selected}"))
                {
                    MessageBuss.MessageReceived += (s, e) =>
                    {
                        switch (e.Message)
                        {
                        case "LoginStateChanged":
                        {
                            if (e.Data == null)
                            {
                                ResetModel();
                                return;
                            }

                            var data = ((string UID, string UserName, string Email))e.Data;

                            string userPath = path.Replace("{user}", data.UID);

                            if (!HasToken(userPath))
                            {
                                SetModel(userPath);
                            }
                        }
                        break;

                        case "ModelSelectionChanged":
                        {
                            var    data = e.Data as JsonModel;
                            string key  = GetSelectedKey(data.Path, path);

                            if (!string.IsNullOrEmpty(key))
                            {
                                string userPath = path.Replace("{selected}", key);

                                if (!HasToken(userPath))
                                {
                                    SetModel(userPath);
                                }
                            }
                        }
                        break;
                        }
                    };
                }
                else
                {
                    SetModel(path);
                }

                // 모델 초기화
                void ResetModel()
                {
                    // array
                    if (IsArrayType(property.PropertyType))
                    {
                        element.SetValue(property, null);
                    }
                    // value
                    else
                    {
                        binding?.RemoveSyncElement(element);
                    }
                }

                // 모델 설정
                void SetModel(string dataPath)
                {
                    ResetModel();

                    // array
                    if (IsArrayType(property.PropertyType))
                    {
                        element.SetValue(property, CreateJsonCollection(datastore, dataPath));
                    }
                    // value
                    else
                    {
                        JsonSingleModel model = CreateJsonPropertyModel(datastore, dataPath);
                        binding = GetDataPropertyBinding(model);

                        binding.AddSyncElement(element, property);
                    }
                }
            }
        }