Databinding Current Item

Since I’ve started in with WPF (2007) I’ve seen many people creating Validation.ErrorTemplates with the following Binding to display the error message

{Binding ElementName=customAdorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}

When this binding is used you will see the following errors appear in the Debug Output window:

System.Windows.Data Error: 16 : Cannot get ‘Item[]’ value (type ‘ValidationError’) from ‘(Validation.Errors)’ (type ‘ReadOnlyObservableCollection`1′). BindingExpression:Path=AdornedElement.(0).[0].ErrorContent; DataItem=’AdornedElementPlaceholder’ (Name=’customAdorner’); target element is ‘TextBlock’ (Name=”); target property is ‘ToolTip’ (type ‘Object’) TargetInvocationException:’System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. —> System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
at System.ThrowHelper.ThrowArgumentOutOfRangeException()
at System.Collections.Generic.List`1.get_Item(Int32 index)
at System.Collections.ObjectModel.Collection`1.get_Item(Int32 index)
at System.Collections.ObjectModel.ReadOnlyCollection`1.get_Item(Int32 index)
— End of inner exception stack trace —
at System.RuntimeMethodHandle._InvokeMethodFast(Object target, Object[] arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeTypeHandle typeOwner)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture, Boolean skipVisibilityChecks)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at MS.Internal.Data.PropertyPathWorker.GetValue(Object item, Int32 level)
at MS.Internal.Data.PropertyPathWorker.RawValue(Int32 k)’

When you change the Binding from

{Binding ElementName=customAdorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}

To

{Binding ElementName=customAdorner, Path=AdornedElement.(Validation.Errors)/ErrorContent}

All the exception related to the [0] will be a thing of the past as the / will bind to the current item of the collection and will not cause a any errors if there is no current item.

This could also be used when you have a collection of items and want to display the current item’s properties on a UI (View).

Hope this post can help other people.

Kind regards,

Johan J v Rensburg

Example Source Code: DataBinding CurrentItem.rename_to_zip

Advertisements

About Johan

.Net Developer
This entry was posted in WPF and tagged . Bookmark the permalink.

3 Responses to Databinding Current Item

  1. Toni Siderov says:

    Thank you Johan,
    The validation trick is neat!
    It makes a real difference on any form with more than couple properties to validate.

    It shifts the usage from an item guarded by exception catching block to one which does not throw exception at all. Catching and ignoring exceptions every now and then makes code more linear and easier to read, but doing it all the time has its cost.

    There are some nuggets of wisdom found only with experience and someone who has the eyes to see them.

  2. mirras says:

    Your solution did not work in my case, but following worked: Text=”{Binding ElementName=customAdorner, Path=AdornedElement.Validation.Errors).CurrentItem.ErrorContent}

    Cheers 😉

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s