Pattern for null settings
-
Hi, I would like to hear your thoughts and ideas about this one. in my application i have controls that are binded to objects properties. but.. the controls always looks like that: a check box, label that explain the settings and then the edited control (for ex: text box) when unchecking the checkbox i disable the text box (using binding) when the checkbox is unchecked i want the property to contain null, and when it is checked i would like the property to contain the text box's text. Of course text box can be NumericUpDown, ComboBox, DatePicker etc.. Do you have any smart way of doing it using binding or do i have to do everything on code; I really would like to a build a control that supports that and re-use it all over Ideas? Thanks,
-
Hi, I would like to hear your thoughts and ideas about this one. in my application i have controls that are binded to objects properties. but.. the controls always looks like that: a check box, label that explain the settings and then the edited control (for ex: text box) when unchecking the checkbox i disable the text box (using binding) when the checkbox is unchecked i want the property to contain null, and when it is checked i would like the property to contain the text box's text. Of course text box can be NumericUpDown, ComboBox, DatePicker etc.. Do you have any smart way of doing it using binding or do i have to do everything on code; I really would like to a build a control that supports that and re-use it all over Ideas? Thanks,
You could use mult-binding to achieve this...something like this should work...here using a multi-binding with a converter on the text property of the TextBlock. Multi-binding allows us to bind (of course) to multi-items and the converter handles the trick of looking at the value of the check box to see if we should return null or the property value.
CheckMe namespace Property { /// /// Interaction logic for Window1.xaml /// public partial class Window1 : Window, INotifyPropertyChanged { String _myProperty; public String MyProperty { get { return _myProperty; } set { _myProperty = value; if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("MyProperty")); } } public Window1() { InitializeComponent(); MyProperty = "Nothing"; DataContext = this; } #region INotifyPropertyChanged Members public event PropertyChangedEventHandler PropertyChanged; #endregion } public class CheckBoxConverter : IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { string name; bool isChecked = (bool)values[0]; String myProperty = values[1] as String; if (!isChecked) name = "not checked"; else name = myProperty; return name;
-
You could use mult-binding to achieve this...something like this should work...here using a multi-binding with a converter on the text property of the TextBlock. Multi-binding allows us to bind (of course) to multi-items and the converter handles the trick of looking at the value of the check box to see if we should return null or the property value.
CheckMe namespace Property { /// /// Interaction logic for Window1.xaml /// public partial class Window1 : Window, INotifyPropertyChanged { String _myProperty; public String MyProperty { get { return _myProperty; } set { _myProperty = value; if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("MyProperty")); } } public Window1() { InitializeComponent(); MyProperty = "Nothing"; DataContext = this; } #region INotifyPropertyChanged Members public event PropertyChangedEventHandler PropertyChanged; #endregion } public class CheckBoxConverter : IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { string name; bool isChecked = (bool)values[0]; String myProperty = values[1] as String; if (!isChecked) name = "not checked"; else name = myProperty; return name;
-
You could use mult-binding to achieve this...something like this should work...here using a multi-binding with a converter on the text property of the TextBlock. Multi-binding allows us to bind (of course) to multi-items and the converter handles the trick of looking at the value of the check box to see if we should return null or the property value.
CheckMe namespace Property { /// /// Interaction logic for Window1.xaml /// public partial class Window1 : Window, INotifyPropertyChanged { String _myProperty; public String MyProperty { get { return _myProperty; } set { _myProperty = value; if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("MyProperty")); } } public Window1() { InitializeComponent(); MyProperty = "Nothing"; DataContext = this; } #region INotifyPropertyChanged Members public event PropertyChangedEventHandler PropertyChanged; #endregion } public class CheckBoxConverter : IMultiValueConverter { public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) { string name; bool isChecked = (bool)values[0]; String myProperty = values[1] as String; if (!isChecked) name = "not checked"; else name = myProperty; return name;
Hi, Thanks again, but actually i have some problems with this. when i uncheck the checkbox the text in the textbox is replaced correctly, but the target property isn't (MyProperty) when i put null in MyProperty the checkbox is'nt unchecked (i changed the ConvertBack to set false if value is null and it doesn't happen) ideas?
-
Hi, Thanks again, but actually i have some problems with this. when i uncheck the checkbox the text in the textbox is replaced correctly, but the target property isn't (MyProperty) when i put null in MyProperty the checkbox is'nt unchecked (i changed the ConvertBack to set false if value is null and it doesn't happen) ideas?
-
Yes, I don't see a direct way to get MyProperty set back when unchecked. I'll have to think about that one.