Find XAML Framework Elements Bounded To An Object - c#

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.

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:
Reflection
MarkupObject / MarkupWriter
TypeDescriptor+DependencyPropertyDescriptor
Custom Binding Markup Extension
Take a look at this link.
http://blog.spencen.com/2008/05/02/how-to-get-a-list-of-bindings-in-wpf.aspx
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.
http://www.codeproject.com/Articles/21139/An-XAML-Serializer-Preserving-Bindings
http://blogs.msdn.com/b/marlat/archive/2009/05/24/getbindingexpression-a-good-way-to-iterate-over-all-dependency-properties-in-visual-tree-in-silverlight-3.aspx
Related links:
Retrieve all Data Bindings from WPF Window
Getting list of all dependency/attached properties of an Object
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/580234cb-e870-4af1-9a91-3e3ba118c89c

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

Related

x:Bind in UWP (Universal Windows Platform)

Right. So moving from WPF to UWP, I'm trying to use x:Bind to get compile-time benefits. Simple scenarios work fine; however I have found a number of issues that I was not able to solve. They are all related, so I thought I'd post them in one place:
I haven't been able to make Intellisense work with x:Bind. I have set DataContext (as well as d:DataContext just as we do in WPF) both in XAML and in the constructor, but it won't show members no matter what. Has anyone done this successfully?
Then I read somewhere that in UWP, DataContext is always set to Page's code-behind (really??) and that I need to define a ViewModel type property in the code-behind and then use that property in x:Bind. Is this correct? I tried it and it works but gives rise to the next question.
If I define a property of ViewModel type in Page's code-behind, Any sub-properties that raise PropertyChanged notifications do not update the UI. For example, if the code-behind property is named Game (of type GameVM) and there is a public property in GameVM named Player (of type GamePlayer), and in turn GamePlayer contains a property named Name, the x:Bind path will look like {x:Bind Path=Game.Player.Name}. But if I do this, any change notifications raised from within Name property do not update Page's UI.
One alternate I tried was to listen to PropertyChanged at each level and then bubble it up the hierarchy, but that hasn't worked. Even if it does, doing this seems a bit too much work. In WPF sub-properties like Game.Player.Name work properly without having to doing property change bubbling. Or am I missing something?
Right. After playing with it for a few days and searching numerous references, here are my findings:
{x:Bind} lacks design-time support. The feature is on the wishlist though. You may want to upvote it there.
(The new version of Visual Studio 15.4.4 does support Intellisense in {x:Bind}in the required way.)
{x:Bind} uses code-behind as its DataContext. So you need to define a public property of your ViewModel type in the code-behind and then use it in your {x:Bind} path.
As pointed out by IInspectable, the default mode for {x:Bind} is OneTime, unlike {Binding} which uses OneWay or TwoWay in almost all cases. So you need to explicitly specify Mode in your binding. People coming from WPF should take special care of it.
Sub-properties that implement notification change work perfectly fine in {x:Bind}. There is no need of bubbling these notifications upwards in the property hierarchy. The problem I was facing (#3 in the question) was because my sub-property was of type List<T>. I changed it to ObservableCollection<T> and it started working.
Hope this works somebody down the road.
Well as a beginner, the only question I can answer for you is the first one. Intellisense does not work inside the {x:Bind}. The members are never shown there in UWP for some unknown reasons. As for the next two questions of yours, I am still working on them.
I ran into the same challenge that you have seen. In my experience, in order to create the compile-time binding and have it update with custom objects as properties, the Page class seems to need to know about the data context and custom objects... all you need to do is reference them in the code behind, and then bind to them in the XAML. This creates the code generation objects it needs.
For example, I have a viewmodel, CustomerViewModel that is bound in XAML. That viewmodel also has a property of type IGuest. In order to use the guest object and have it update properly, I came up with this in the code behind...
CustomerViewModel vm
{
get
{
return (CustomerViewModel)DataContext;
}
}
IGuest g
{
get
{
return vm.CurrentGuest;
}
}
public CartGuestControl()
{
this.InitializeComponent();
}
You don't need to assign any of the UI data contexts from the code behind... simply reference the datacontext that is bound in XAML. When binding to any straight viewmodel properties, I use {x:Bind Path=vm.IsEditing, Mode=OneWay}. For binding to any of the guest properties, it looks like this, {x:Bind Path=g.FirstName, Mode=TwoWay}. You could do something like this for your Player object.
I have run into times where x:Bind simply won't do what I expect it to do no matter what I try. This can usually be solved by breaking things out into smaller user controls with more specific data contexts or by using "regular" Binding.

Making a grid of objects

I'm trying to make something like a quiz application where 3 questions will be brought up on screen at a time, allowing the user the check a radio button containing "Yes" or "No", and have an answer come up appropriately to his response. The questions will continually come from a database I'm using.
After a few attempts I've figured that using x:Name property is not a great solution, since it doesn't allow me to use a loop in order to change the questions and answers. Is there any other way to make a grid with the same types of objects in each cell, being able to access each object inside each cell of the grid in the code-behind?
Here is list of steps you need to implement,
Need to create QuestionModel, contains question properties, make sure your model inherits INotifyPropertyChanged.
Need to create ViewModel, which contains data objects, public/dependency properties
Need to bind/set data objects/properties on viewmodel constructor
Need to set your ViewModel as a DataContext of your View(.xaml) (You can create this on zammel directly and codebehind as well
Need to bind your UI objects like Question/answers/yes-no with viewmodel properties accordingly
WPF/Silverlight has their own fundamentals like Data Binding, Resources, Compiler, Dependency Properties. Above steps comprises MVVM design pattern. During each steps, please google for specific stuff.

How to dynamically update a listviewitem through code

I have a listview and would like to update the text of one of the columns for a specific listviewitem (row).
How would I go about doing this?
Hard to say without any context because there are so many ways you could populate your list!
The generic answer is you bind your list to a collection view which itself binds its source to your viewmodel (or you bind directly to your viewmodel if you don't need CollectionView features).
When you want to modify your list, you make sure you raise a modification notification on your property, and XAML binding will take care of updating everything.
It is really basic stuff on dependency property and binding, you should read more about this topic. MVVM-light is a very light framework that allows you to take care of all kinds of binding-related issues with a very clean and neat flavor. You will also find some very good self-explanatory webcasts from the author of the website about all those topics.

Is there a data-driven WPF property grid/editor available? (preferably free)

I'm looking for a data-driven WPF property grid/editor.
Most property grids appear to work using reflection to figure out the CLR properties on your class. Those properties are then displayed in the grid with editors that are specific for the type of the property.
However I don't have a specific class that I want to plug into the property grid. All I have is data, specifically a collection of name/value pairs. I want to put this collection into the grid and have a editor that is specific for the value type of each pair.
Does anyone know of an existing property grid/editor control that supports data-driven properties?
Also if it looks like the property grid/editor in Blend I won't complain ;)
I've never used it, but from what I've read about the PropertyTools for WPF on codeplex.com supports dynamically editting the current selected object.
http://propertytools.codeplex.com/

Caliburn.Micro & Infragistics XamDockManager + TabGroupPane

I'm trying to get the Infragistics TabGroupPane to integrate with Caliburn.Micro as per the standard WPF TabControl.
I've tried adding a new convention in the bootstrapper (a modification of the standard WPF one) but have not had any success.
My TabGroupPane is defined as:
<igDock:TabGroupPane x:Name="Items"/>
When an item is added to the collection an exception is thrown "InvalidOperationException: A TabGroupPane can only contain 'ContentPane' and 'ContentPanePlaceholder' instances".
Also, if my TabGroupPane is inside a DockManager:
<igDock:XamDockManager>
<igDock:DocumentContentHost>
<igDock:SplitPane>
<igDock:TabGroupPane x:Name="Items" />
</igDock:SplitPane>
</igDock:DocumentContentHost>
</igDock:XamDockManager>
It doesn't appear to be found by Caliburn at all.
Has anyone else done this before?
Cheers!
Update
I have created my own dock manager class, a blog post explaining it or just the code on bitbucket. Hopefully this will be useful to someone!
If you can't get the ContentPaneFactory suggested in Infragistics blog to fill the TabGroupPane through binding, you might consider these alternative approaches:
keep your VM with BindableCollection in place, subscribe VM change notification in code-behind and change the TabGroupPane programmatically according to the actual change occurred in observed collection
build a custom IResult and invoke it from VM (using Coroutine.Execute) according to the desired TabGroupPane variation.
In IResult.Execute body you have access to ActionExecutionContext.View, so you can reach the TabGroupPane by name and operate whathever action you need on it.
This way you keep your VM free from direct reference to the particular UI control library.
define an interface abstracting the whole Docking Manager, and use it as a service from VM (see this post about the implementation of a Docking Manager). This approach diverges quite a lot from the MVVM idea, but unfortunately most docking library are not very MVVM friendly...
You can add bespoke controls to the ConventionManager class in Caliburn Micro, I would simply follow the example of the standard TabGroupPanel.
I would be wary of amending the actual class though, If I remember rightly (there is a recent post on the discussion forums at CaliburnMicro Codeplex page) You can also add bespoke conventions to the bootstrapper by overriding the Config method? I say this because if you amend the class itself, you will tie yourself to a particular version of Caliburn.
Its a really simple framework and easy to get to know intimately, I would recommend stepping through the code thats run when you bind a view to a viewmodel, there you will learn how these conventions are setup.
Have you tried using a more explicit binding? Caliburn can only do automatic binding on certain elementtypes, and I guess Infragistics TabGroupPane is not one of them!
I guess you have to use something like this:
<igDock:TabGroupPane x:Name="Items" ItemSource={Binding <what to bind to>}/>
Note: This is just used as an example, not sure if ItemSource is the correct property!

Categories

Resources