1

I need to let the Exception that happens during data bind to bubble up and not get swallowed by WPF. Usually this error would be in Output and that is enough but in my case I need to let it bubble up.

I have a property defined as follows:

public string Text
{
    get
    {
        return (string)GetValue(TextProperty);
    }
    set
    {
        SetValue(TextProperty, value);
    }
}
public static readonly DependencyProperty TextProperty = DependencyProperty.Register(nameof(Text), typeof(string), typeof(EntitySearch), new PropertyMetadata(string.Empty, TextChangedCallback));

public static void TextChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
    if (DesignerProperties.GetIsInDesignMode(d))
        return;

    if (d is EntitySearch es)
    {
        es.m_key = e.NewValue;
        object obj = es.m_context.Set(es.m_entityType).Find(e.NewValue);
        if (obj.IsEmpty())
            throw new Exception("Entity with this key does not exist.");
        es.SelectedObject = obj;
    }
}

This is your usual Text property of an input control but in my case this value is used as a key to fetch something from the database. As you can see I would like to throw an exception here and let that exception bubble up. Eventually someone up the stack would process the exception and show it to the user.

Any suggestions?

I like to note I would, if possible, like to keep the concept of throwing the exception rather than setting some property to false and than later on processing this property in some MVVM fashion.

Robert
  • 2,407
  • 1
  • 24
  • 35
  • Possible duplicate of: http://stackoverflow.com/questions/4225867/how-can-i-turn-binding-errors-into-runtime-exceptions – Mishka May 15 '17 at 14:58
  • It is not a duplicate. In that question answer will make all the binding errors become a runtime errors where as in my question I want just one property to propagate the exception and not swallow it. – Robert May 16 '17 at 07:00
  • Sorry didn't mean to suggest anything by it. I simply believe you could derive a solution to your problem from that answer. – Mishka May 16 '17 at 08:39

0 Answers0