public BoolSetting(string aName, string aCaption, bool?aDefaultValue)
 {
     Name            = aName;
     Caption         = aCaption;
     DefaultValue    = aDefaultValue;
     _controlBinding = new CheckBoxBinding(this);
 }
        /// <summary>
        /// Builds a new binding.
        /// </summary>
        /// <typeparam name="TView">The type of the view the new binding binds to.</typeparam>
        /// <typeparam name="TControl">The type of the control the new binding binds to.</typeparam>
        /// <typeparam name="TViewModel">The type of the viewmodel the new binding binds to.</typeparam>
        /// <param name="view">The view to bind to.</param>
        /// <param name="control">The control to bind to.</param>
        /// <param name="viewProperty">The binding target property (the property on the control to bind to).</param>
        /// <param name="viewModel">The viewmodel to bind to.</param>
        /// <param name="viewModelProperty">The binding source property (the property on the viewmodel to bind to).</param>
        /// <returns>A new binding.</returns>
        internal static Binding <TView> Build <TView, TControl, TViewModel>(TView view,
                                                                            TControl control, Expression <Func <TControl, object> > viewProperty, TViewModel viewModel, Expression <Func <TViewModel, object> > viewModelProperty) where TViewModel : INotifyPropertyChanged
        {
            Binding <TView> result = null;

            // special cases are handled by type and property name
            if (typeof(TControl) == typeof(TextBox) && control.GetPropertyInfo(viewProperty).Name == "Text")
            {
                result = new TextBoxBinding <TView, TViewModel>(view, control as TextBox,
                                                                viewProperty as Expression <Func <TextBox, object> >, viewModel, viewModelProperty);
            }

            if (typeof(TControl) == typeof(CheckBox) && control.GetPropertyInfo(viewProperty).Name == "Checked")
            {
                result = new CheckBoxBinding <TView, TViewModel>(view, control as CheckBox,
                                                                 viewProperty as Expression <Func <CheckBox, object> >, viewModel, viewModelProperty);
            }

            if (typeof(TControl) == typeof(ComboBox) && control.GetPropertyInfo(viewProperty).Name == "SelectedIndex")
            {
                result = new ComboBoxBinding <TView, TViewModel>(view, control as ComboBox,
                                                                 viewProperty as Expression <Func <ComboBox, object> >, viewModel, viewModelProperty);
            }


            if (typeof(TControl) == typeof(DateTimePicker) && control.GetPropertyInfo(viewProperty).Name == "Value")
            {
                result = new DateTimePickerBinding <TView, TViewModel>(view, control as DateTimePicker,
                                                                       viewProperty as Expression <Func <DateTimePicker, object> >, viewModel, viewModelProperty);
            }


            if (typeof(TControl) == typeof(RadioButton) && control.GetPropertyInfo(viewProperty).Name == "Checked")
            {
                result = new RadioBinding <TView, TViewModel>(view, control as RadioButton,
                                                              viewProperty as Expression <Func <RadioButton, object> >, viewModel, viewModelProperty);
            }

            if (result != null)
            {
                result.HookEvents();
                return(result);
            }

            // generic one-way binding
            return(new SimpleBinding <TView, TControl, TViewModel>(view, control,
                                                                   viewProperty, viewModel, viewModelProperty));
        }
示例#3
0
        public ConfigurationGuiBinding BindBoolean(Control control, string property, bool defaultValue)
        {
            CheckBox checkBox = control as CheckBox;

            if (checkBox != null)
            {
                CheckBoxBinding binding = new CheckBoxBinding(checkBox, defaultValue);
                AddBinding(property, binding);
                checkBox.CheckedChanged += ControlValueChanged;
                return(binding);
            }
            else
            {
                throw new ApplicationException("Cannot bind " + control.GetType().Name + " to bool property.");
            }
        }