Can I handle thrown exceptions in XAML?

In my XAML, I get all clients by binding the GetAll property:

<ListBox ItemsSource="{Binding GetAll}" ItemTemplate="{StaticResource allCustomersDataTemplate}" Style="{StaticResource allCustomersListBox}"> </ListBox> 

The GetAll property is an observable collection in my view model, which invokes the model to retrieve the entire collection of clients:

 public class CustomersViewModel { public ObservableCollection<Customer> GetAll { get { try { return Customer.GetAll; } catch (Exception ex) { throw new Exception(ex.Message); } } } } 

If something goes wrong in the model (poorly formed XML file, etc.), then the exception bubbles up to the full GetAll property in the ViewModel.

First question: I was surprised that XAML does not seem to do anything with the exception and just continues and does not display anything. Is it for design? Is this part of the “untied approach”?

Second question: It makes me think that I could somehow handle the exception in XAML , for example

Pseudocode:

 <Trigger> <Trigger.OnException> <TextBox Text="The customer data could not be loaded."/> </Trigger.OnException> </Trigger> 

Is something like the above code possible?

+4
source share
4 answers

First, I would suggest that it is not supposed that XAML exceptions should be detected. They exist more as a tool to help the developer understand how they need to fix their XAML code, although they should, of course, happen at runtime (initialization) due to the dynamic nature of the XAML markup

Talking about this, you can easily handle XAML exceptions by completing the InitializeComponents call in the constructor of your Windows class. You can then either catch all exceptions, or specifically a XamlParseException , depending on what you think is appropriate.

Example of this blog post :

 public partial class Window1 : System.Windows.Window { public Window1() { try { InitializeComponent(); } catch (Exception ex) { // Log error (including InnerExceptions!) // Handle exception } } } 
+4
source

You can use FallBackValue to provide a value for use in binding if an error occurs. Besides using FallBackValue, you cannot handle exceptions in xaml.

You can also see TargetNullValue , which make the specific value equivalent to Null (for example, if you set TargetNullValue = 5 and your user enters 5, your setter will receive Null, and if your recipient receives Null, a binding of 5 will be displayed).

+2
source

My first thought is that you can use a ValueConverter , check for value==null , and then set some arbitrary property of the view model through the converter parameter. Then you can use the trigger of the correct property in your xaml to show the error.

I'm sure you could do this with the binding validator, although I would look there first.

EDIT: Yes, look at http://msdn.microsoft.com/en-us/library/ms753962.aspx for introduction of binding verification rules that will put you on the right path

+1
source

Yes, you can handle the exception using XAML. It exists for unhandled exceptions. This link may help .. http://www.wpf-tutorial.com/wpf-application/handling-exceptions/

0
source

All Articles