示例#1
0
        public DataPropertyBinding(JsonSingleModel model)
        {
            syncList = new Dictionary <FrameworkElement, DependencyProperty>();

            this.Model = model;
            this.Model.RemotePropertyChanged += Model_RemotePropertyChanged;
        }
示例#2
0
        private static DataPropertyBinding GetDataPropertyBinding(JsonSingleModel model)
        {
            if (!dataBindingCache.TryGetValue(model, out DataPropertyBinding binding))
            {
                binding = new DataPropertyBinding(model);
                dataBindingCache[model] = binding;
            }

            return(binding);
        }
示例#3
0
        private static JsonSingleModel CreateJsonPropertyModel(IDatastore datastore, string path)
        {
            if (!modelCache.TryGetValue(path, out JsonSingleModel model))
            {
                model            = new JsonSingleModel(datastore, path);
                modelCache[path] = model;
            }

            return(model);
        }
示例#4
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);
                    }
                }
            }
        }