Binding: Accept an empty string in enumeration [modified]
-
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 aTextbox.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 theIsUsed
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 theConvertBack
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 aDataTemplate
sandbox forItemsControl.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
-
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 aTextbox.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 theIsUsed
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 theConvertBack
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 aDataTemplate
sandbox forItemsControl.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
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
-
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
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