Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Xaml Binding to empty collection

$
0
0

Hello

I have an entity called Employee with an ObservableCollection<Address> and Address has several properties eg. Town.

Now I'd like to bind directly to the Town value as in

<TextBox Text="{Binding Path=Employee.Addresses[0].Town" />

If Addresses is empty, I get as expected
System.Windows.Data Error: 17 ... ArgumentOutOfRangeException

The application continues running without problems, but an exception is an exception.

I know, I could define in my ViewModel a new string property TownValue, verify if Addresses[0] exists and then assign that value to TownValue and set the Binding to that property.

But I'd prefer some solution on the xaml/Binding side.

  • A new collection inheriting from ObservableCollection with an overriden indexer?
  • Converter with the collection and the index as parameters?
  • Some MarkupExtension to extend the Binding?

Any hints?


Viewing all articles
Browse latest Browse all 18858

Trending Articles