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.