0

I have :

 public class Person : INotifyPropertyChanged
    {
       private string _name;

        public int Age { get; set; }

        public string Name
        {
            get { return _name; }
            set
            {
                if (!String.IsNullOrEmpty(_name))
                {
                    if (String.IsNullOrEmpty(value))
                    { 
                       throw new Exception("name couldn't be null");



                    }
                    else if ((_name.Equals(value) != true))
                    {
                        if (!String.IsNullOrEmpty(value))
                        {
                            throw new Exception("name couldn't be null");
                        }
                        else
                        {
                            InvokePropertyChanged("_name");
                        }


                        _name = value;
                    }

                }

                else if (String.IsNullOrEmpty(value))
                {
                    throw new Exception("name couldn't be null");
                }
                else
                {
                    _name = value;
                }

            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void InvokePropertyChanged(string propertyName)
        {

            var e = new PropertyChangedEventArgs(propertyName);

            PropertyChangedEventHandler changed = PropertyChanged;

            if (changed != null) changed(this, e);

        }

>
<Grid>
    <StackPanel>
        <TextBox Name="tbName" Text="{Binding Path=Name, Mode=TwoWay}"></TextBox>
        <TextBox Name="tbOther" Text="Come in"></TextBox>
    </StackPanel>
</Grid>

and

 public UserControl1()
        {
            InitializeComponent();

            Person person = new Person();
            person.Name = "Patrick";

 this.DataContext = person;

        }

Why when I debug and enter to line of :

throw new Exception("name couldn't be null");

exception is not showing.

user278618
  • 19,306
  • 42
  • 126
  • 196
  • Are you sure your debug db (.pdb) is updated? You might want to do a complete rebuild. Also, when you say that exception is not showing, what do you mean? Where is it not showing? – Prashant Jun 28 '10 at 13:08
  • It means: I am at line throw new Exception("name couldn't be null") and I press F10 and nothing happens. Application still work. – user278618 Jun 28 '10 at 13:13

4 Answers4

2

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

Community
  • 1
  • 1
CodeNaked
  • 40,753
  • 6
  • 122
  • 148
1

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
  • 3,224
  • 3
  • 27
  • 34
1

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
  • 57,579
  • 7
  • 124
  • 111
  • @phenevo: You can set Validation.ErrorTemplate to a different ControlTemplate if you want it to display something other than a red border. – Quartermeister Jun 28 '10 at 14:56
1

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
  • 937
  • 1
  • 10
  • 23
  • well tht means the exception is being thrown properly. Now exactly what do you want to do with it? – Prashant Jun 28 '10 at 13:47
  • 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 :/ – user278618 Jun 28 '10 at 13:53
  • 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 Jun 28 '10 at 14:12