views:

67

answers:

4
+1  A: 

You're not setting the control to be bound to your person object from what I can see. As such, it shouldn't be attempting to set it and subsequently erroring.

When you put a break point in your getter, does it even get called?

JustLoren
sorry, I forgot about past line this.DataContext = person;. So , yes I get into getter.
+1  A: 

I think you want to assign the Person object to the control's DataContext:

public UserControl1()
{
    InitializeComponent();
    Person person = new Person();
    person.Name = "Patrick";
    this.DataContext = person;
}

You may also want to set ValidatesOnExceptions on your binding so that the UI will display the error template when an exception is thrown in the setter.

<TextBox Name="tbName" Text="{Binding Path=Name, Mode=TwoWay, ValidatesOnExceptions=True}"></TextBox>
Quartermeister
Very useful, but it only mark my textbox as red.
@phenevo: You can set Validation.ErrorTemplate to a different ControlTemplate if you want it to display something other than a red border.
Quartermeister
+1  A: 

Please see this other question, but basically the exception will be handled and suppressed by the Binding.

Tom Goff
+1  A: 

try doing this:

...
else if (String.IsNullOrEmpty(value))
{
    try{
        throw new Exception("name couldn't be null");
    }
    catch(Exception ex)
    {
         //Set Breakpoint Below
         int x=0;
    }
}
...

You will see that your code will come in the catch block, and the exception does get thrown. The handling of the exception however is upto you. The best way is already suggested by Quartermeister.

Otherwise based on your VS IDE settings, the exception gets suppressed, and you do not see an error on screen.

Prashant
it enters into catch :/
well tht means the exception is being thrown properly. Now exactly what do you want to do with it?
Prashant
No exacly. I run .exe and when I enter wrong data in filed I don't het exception. So it doesn'y depend of my setting in Visual Studio. Normally I would get info about exception. I miss it :/
The exception is supressed at the binding level.You can show the info manually in the Catch block by using catch(Exception ex) { Messagebox.Show("Exception Desc: " + ex.ToString()); }
Prashant