0

I have a Property which looks like this:

private int _wertungvalue;
public int WertungValue
{
    get { return _wertungvalue; }
    set
    {
        _wertungvalue = value;
        RaisePropertyChanged(() => WertungValue);
    }
}

It's binded to a TextBox

<TextBox Text="{Binding WertungValue, Mode=TwoWay}"/>

So the user can type in anything he wants (I don't want a numeric textbox!). The user types in '5' and the value of WertungValue is 5. If the user types in 'Test', a red border pops up and the value of WertungValue is still 5!

Now I also have a RelayCommand

RelayCommand(DeleteExecute,CanDelete);

In CanDelete, I check if the propery is an int

private bool CanDelete()
{
    int ot = 0;

    if (int.TryParse(WertungValue.ToString(),out ot) == false)
        return false;
    else
        return true;
}

so that the RelayCommand should only work, when the value is an integer. So this means when the user types in 'Test', RelayCommand should return false. The problem is that it will never return false, since the value of the property is always an integer, but in the View it's a string.

I don't want to make the property to type string or use UpdateSourceTrigger=PropertyChanged in my TextBox. I also don't want to make a numeric only TextBox... The user should be allowed to type in anything he wants, but the RelayCommand should only work when he types an integer.

Rudi
  • 926
  • 2
  • 19
  • 38
  • You can't do what you want. You'll get a binding error and your "set" will never get called on your property. Setting `UpdateSourceTrigger` to "PropertyChanged" doesn't do anything to resolve the issue. You don't need to make your single property a string...but you should add a second property that you bind your textbox to. – Thelonias Jun 12 '13 at 12:45

1 Answers1

1

I don't think that this is possible with an int-property. When the Binding updates the property, it tries to convert the value to an integer, which causes an exception when the user enters 'Test', thus the value is never updated and stays 5.

I think you need two properties: One of type string, and one of type int (or maybe int?). The textbox is bound to the string property, and in the setter you can check if the value can be parsed and update the int property accordingly.

Daniel Sklenitzka
  • 2,116
  • 1
  • 17
  • 25
  • K, thx. But I'm curious how the view checks the Binding type. It has to make a TryParse (or whatever) to check the type of the input. Instead of doing it in the property, it does it in...?? I mean there has to be something between the Binding and the Property to check the type of the input. – Rudi Jun 12 '13 at 13:27
  • It checks the type of your property and sees 'int'. Therefore it needs to convert the string to an integer before it can call the setter. If this conversion fails, an exception is thrown and the setter of your property is never invoked. – Daniel Sklenitzka Jun 13 '13 at 08:40
  • Is there any way to access this exception (in my ViewModel)? – Rudi Jun 13 '13 at 08:44
  • 1
    Here are some possible solutions: http://stackoverflow.com/questions/13335787/wpf-data-binding-exception-handling – Daniel Sklenitzka Jun 13 '13 at 09:44