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. WPF
  4. Binding: Accept an empty string in enumeration [modified]

Binding: Accept an empty string in enumeration [modified]

Scheduled Pinned Locked Moved WPF
wpfwcfjsonhelptutorial
3 Posts 2 Posters 1 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.
  • L Offline
    L Offline
    Lutoslaw
    wrote on last edited by
    #1

    Hello! I have a class:

    class MyClass
    {
    public enum Letters {A,B,C}
    public Letters Letter {get;set;}
    public bool IsUsed { get; set; }
    // lots of other data
    }

    Now, I'd like to bind the Letter property to a Textbox.Text. Works fine, but if a user left the textbox empty, then a parsing error would occur. Instead, in that case I would like to set the IsUsed property to false. I do not want to have a combo box + checkbox, because the enum values are short and well known (like letters) and writing them in a text box is most convenient. I had tried to bind whole MyClass object and use a converter, but the ConvertBack method didn't have enough data to generate a MyClass object from scratch (there are other members than Letter and IsUsed). I had also tried to pass the original object as a converter parameter but it came out that I cannot bind to ConverterParameter so I am in a dead point. NOTE: Everything happens in a DataTemplate sandbox for ItemsControl.ItemTemplate, like here:

        <DataTemplate x:Key="DataTemplate\_Level2">
            <TextBox Text="{Binding Path=Letter, Mode=TwoWay}"  Width="40" Margin="2" />
        </DataTemplate>
    

    How to get it working? Thanks in advnace

    Greetings - Jacek

    modified on Saturday, July 30, 2011 4:36 PM

    T 1 Reply Last reply
    0
    • L Lutoslaw

      Hello! I have a class:

      class MyClass
      {
      public enum Letters {A,B,C}
      public Letters Letter {get;set;}
      public bool IsUsed { get; set; }
      // lots of other data
      }

      Now, I'd like to bind the Letter property to a Textbox.Text. Works fine, but if a user left the textbox empty, then a parsing error would occur. Instead, in that case I would like to set the IsUsed property to false. I do not want to have a combo box + checkbox, because the enum values are short and well known (like letters) and writing them in a text box is most convenient. I had tried to bind whole MyClass object and use a converter, but the ConvertBack method didn't have enough data to generate a MyClass object from scratch (there are other members than Letter and IsUsed). I had also tried to pass the original object as a converter parameter but it came out that I cannot bind to ConverterParameter so I am in a dead point. NOTE: Everything happens in a DataTemplate sandbox for ItemsControl.ItemTemplate, like here:

          <DataTemplate x:Key="DataTemplate\_Level2">
              <TextBox Text="{Binding Path=Letter, Mode=TwoWay}"  Width="40" Margin="2" />
          </DataTemplate>
      

      How to get it working? Thanks in advnace

      Greetings - Jacek

      modified on Saturday, July 30, 2011 4:36 PM

      T Offline
      T Offline
      teejayem
      wrote on last edited by
      #2

      Making the letter property nullable will fix your paticular issue. But, what if someone types "D"? I would personally use a Selector (like combobox) so the user can choose a valid Letter instead of guessing which letters are valid.

      Don't be overcome by evil, but overcome evil with good

      L 1 Reply Last reply
      0
      • T teejayem

        Making the letter property nullable will fix your paticular issue. But, what if someone types "D"? I would personally use a Selector (like combobox) so the user can choose a valid Letter instead of guessing which letters are valid.

        Don't be overcome by evil, but overcome evil with good

        L Offline
        L Offline
        Lutoslaw
        wrote on last edited by
        #3

        Thanks for answer.

        teejayem wrote:

        Making the letter property nullable will fix your paticular issue.

        This would require altering application logic, since the examplary "Letter" property is used in later processing and a new 'null' value would have to be handled. But since it is my own project it is thinkable. EDIT: No way. It would require too many changes. The enum value is often treated as an integer and sometimes integer arithmetic is used.

        teejayem wrote:

        I would personally use a Selector (like combobox) so the user can choose a valid Letter instead of guessing which letters are valid.

        The code which I have posted is purely exmaplary. As I said, actual enum members are known and obvious for a target user. Anyway thanks for a suggestion, I will consider this option.

        Greetings - Jacek

        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