Saber Amani
Saber Amani

Reputation: 6489

Find XAML Framework Elements Bounded To An Object

How can I find and access to Elements which are bind to an object in XAML ?

Edit : Let's say I have a EmployeeViewModel which is assigned to EmployeeView's DataContext and a EmployeeModel inside my EmployeeViewModel, I want to know which properties of my model bounded to View's Framework Elements (Controls) also I want to have an access to each control bounded to my model properties.

Upvotes: 1

Views: 827

Answers (2)

Colin Smith
Colin Smith

Reputation: 12540

UPDATE: In light of the question being clarified by SaberAmani in that he is trying to add validation to his models and show a validation summary..see the links below.

http://msdn.microsoft.com/en-us/magazine/ff714593.aspx

http://codeblitz.wordpress.com/2009/05/12/wpf-validation-summary-control/

http://wpfvalidation.codeplex.com/

http://f10andf11.blogspot.co.uk/2012/02/wpf-validation-summary-control.html


For reference for people that want to discover Bindings:

You don't mention if your XAML is in WPF, Silverlight, Metro or Phone7 (thus you may be more restricted in what you can do).

There seem to be a few possible ways to do what you want:

  1. Reflection
  2. MarkupObject / MarkupWriter
  3. TypeDescriptor+DependencyPropertyDescriptor
  4. Custom Binding Markup Extension

Take a look at this link.

He uses reflection and suggests this is the classical way to do it...but also mentions MarkupWriter as another possibility. NOTE: the reflection method doesn't discover Attached Properties which may have bindings.

Here are some links that use MarkupWriter...this would allow you to discover the attached properties.

Related links:

Upvotes: 2

GameAlchemist
GameAlchemist

Reputation: 19294

you could use reflection to loop through properties and use FrameworkElement.GetBindingExpression on each property to build, for a given Framework element, all its bindings.

http://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.getbindingexpression

Upvotes: 2

Related Questions