Пример #1
0
        public Where Binding(object value)
        {
            BindingComponent binding = BindingComponent.Binding();

            _components.AddBinding(binding.value, value);
            _components.Add(binding);
            return(this);
        }
Пример #2
0
        public static BindingComponent Binding()
        {
            BindingComponent binding = new BindingComponent($"@binding{_bindingIndex}");

            if (_bindingIndex + 1 == int.MaxValue)
            {
                _bindingIndex = -1;
            }
            _bindingIndex += 1;
            return(binding);
        }
Пример #3
0
        public DataBindingService Bind <T>(string branch, BindingComponent <T> component)
        {
            if (!string.IsNullOrEmpty(branch))
            {
                var data = GetData <T>(branch);
                if (data == null)
                {
                    AddData <T>(branch, default(T), true);
                    data = GetData <T>(branch);
                }

                if (!data.bindedComponents.Contains(component))
                {
                    data.bindedComponents.Add(component);
                    component.OnValueChanged(data.branch, data.value);
                }
            }

            return(this);
        }
Пример #4
0
        private async Task<FrameworkElement> GetControlFrom(BindingComponent bind, Type modelType)
        {

            PropertyInfo info = modelType.GetProperty(bind.PropertyName);


            Control control = new TextBox();
            TextBlock label = new TextBlock() { Text = bind.DisplayName };

            control.SetBinding(TextBox.TextProperty, info.Name);

            if (info.PropertyType == typeof(DateTime))
            {
                control = new DatePicker() { DisplayDateStart = new DateTime(1960, 1, 1)};
                if (!IsEdit)
                {
                    info.SetValue(Item, DateTime.Now);
                }

                control.SetBinding(DatePicker.SelectedDateProperty, info.Name);
            }

            if (info.PropertyType.IsEnum)
            {
                var list = Enum.GetNames(info.PropertyType);
                control = new ComboBox();

                if (list.Length > 0)
                {
                    (control as ComboBox).ItemsSource = list;
                    (control as ComboBox).SetBinding(ComboBox.SelectedItemProperty, bind.PropertyName);

                    if (!IsEdit)
                    {
                        (control as ComboBox).SelectedIndex = 0;
                        info.SetValue(Item, Enum.Parse(info.PropertyType, (control as ComboBox).SelectedItem.ToString()));
                    }
                    else
                    {
                        var value = info.GetValue(Item);
                        (control as ComboBox).SelectedItem = Enum.GetName(info.PropertyType, value);
                    }
                }
            }

            if(bind.PropertyType == PropertyType.OuterPropertyId || 
                bind.PropertyType == PropertyType.OuterPropertyIdNonVisible)
            {
                control = new ComboBox();

                string outerPropName = bind.PropertyName.TrimEnd('I', 'd');
                var outerProp = modelType.GetProperty(outerPropName);

                await allDbContext.Set(outerProp.PropertyType).LoadAsync();

                var list = await allDbContext.Set(outerProp.PropertyType).ToListAsync();

                if (list.Count > 0)
                {

                    (control as ComboBox).ItemsSource = list;
                    (control as ComboBox).SelectedValuePath = "Id";
                    (control as ComboBox).DisplayMemberPath = bind.DisplayMember;

                    (control as ComboBox).SetBinding(ComboBox.SelectedValueProperty, bind.PropertyName);

                    if (!IsEdit)
                    {                        
                        (control as ComboBox).SelectedIndex = 0;
                        info.SetValue(Item, (control as ComboBox).SelectedValue);
                    }
                }
            }

            control.Margin = new Thickness(0, 5, 0, 0);
            label.HorizontalAlignment = HorizontalAlignment.Left;


            StackPanel stack = new StackPanel();
            stack.Children.Add(label);
            stack.Children.Add(control);
            stack.Margin = new Thickness(0, 15, 0, 0);

            return stack;
        }