Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WCF and WF
  4. Pattern for null settings

Pattern for null settings

Scheduled Pinned Locked Moved WCF and WF
wpfwcfregexquestiondiscussion
6 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Y Offline
    Y Offline
    yanairon
    wrote on last edited by
    #1

    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,

    L 1 Reply Last reply
    0
    • Y yanairon

      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,

      L Offline
      L Offline
      lneir
      wrote on last edited by
      #2

      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;

      Y 2 Replies Last reply
      0
      • L lneir

        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;

        Y Offline
        Y Offline
        yanairon
        wrote on last edited by
        #3

        Thanks a lot, it's brilliant. Just one question, why is the binding to the checkbox is one way? when changing a value to null i wish the checkbox to uncheck itself

        1 Reply Last reply
        0
        • L lneir

          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;

          Y Offline
          Y Offline
          yanairon
          wrote on last edited by
          #4

          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?

          L 1 Reply Last reply
          0
          • Y yanairon

            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?

            L Offline
            L Offline
            lneir
            wrote on last edited by
            #5

            Yes, I don't see a direct way to get MyProperty set back when unchecked. I'll have to think about that one.

            Y 1 Reply Last reply
            0
            • L lneir

              Yes, I don't see a direct way to get MyProperty set back when unchecked. I'll have to think about that one.

              Y Offline
              Y Offline
              yanairon
              wrote on last edited by
              #6

              Thanks, I will appreciate if anyone have an idea :) it's a tricky one

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups