Update all bindings in UserControl at once - wpf

I need to update all the bindings on my UserControl when its visibility changes to Visible. Pretty much all my bindings are bound to the DataContext property of the user control so I'm trying to update the target of that binding:
BindingOperations.GetBindingExpressionBase(this, UserControl.DataContextProperty).UpdateTarget();
But I get null as the result of GetBindingExpression(..) method and I'm wondering if I'm using this wrong.
Also, is there any other good way to refresh all bindings on the control (which use DataContext as the source).

Well, you could just re-assign the DataContext:
var dataContext = DataContext;
DataContext = null;
DataContext = dataContext;
FYI, resetting the property to its value (i.e.DataContext = DataContext) won't work.

You're using the BindingOperations.GetBindingExpressionBase method on the wrong property. You have to use it on the properties which are binding to the DataContext property, not the DataContext property itself.

Related

Caliburn.Micro, how to access the actual viewmodel used from within the view

I want to access the actual viewmodel that is currently used from within the view (code-behind). In the bootstrapper I have the viewmodel set to perrequest so I cannot use IoC.Get<..ViewModel>(); (nor do I want to change this behavior).
Basically, I'm looking for the equivalent of the GetView from the Screen, but then the other way around.
DataContext will give you the current ViewModel which is applied as DataContext of view.
// Get you the object of ViewModel.
var viewModelInstance = DataContext;
// Or typecast to exact instance what you intend to use.
MyViewModel vm = DataContext as MyViewModel;
Bear in mind that the DataContext will have a value assigned to it once the View has been loaded. For example you can access to it in the loaded event of the View.

UserControl's DataContext update WPF

I have my own usercontrol named FlashControl in the mainwindow. I set the DataContext by following code in the mainwondow
(FlashControl.Content as FrameworkElement).DataContext = null;
(FlashControl.Content as FrameworkElement).DataContext = this.DataContext;
FlashControl.DataContext = this.DataContext;
My problem is whenever my datacontext change I need to call the above code to reset usercontrol's datacontext. Why Usercontrol's DataContext not updated automatically when main DataContext change? How to do automatic update? Am I missing something?
If you want automatic update dont set DataContext directly but Bind it to the value you want.
You should bind in xaml but if you want to do in code behind then you can do:
Binding myBinding = new Binding("DataContext");
myBinding.Source = this;
BindingOperations.SetBinding(FlashControl, FrameworkElement.DataContextProperty, myBinding);

How to set ValidatesOnDataErrors from codebehind

In WPF how do I set the ValidatesOnDataErrors property for the binding on a control (e.g. a TextBox)? Is this possible?
Thanks!
It's just a property of the Binding class. You can construct bindings in code, set the property and use SetBinding on the TextBox.
You can use GetBinding to get existing bindings, but you cannot modify them once they are in use...
Remember that ValidatesOnDataErrors is a property of a binding, not of a control.
So look for the correct binding of the control (in my example, the TextProperty dependency property)...
Try this:
System.Windows.Data.BindingExpression binding = this.textBox1.GetBindingExpression(System.Windows.Controls.TextBox.TextProperty);
binding.ParentBinding.ValidatesOnDataErrors = true;

Silverlight Behavior: DataContext of AssociatedObject changes

I've written an Interactivity Behavior (from Blend SDK) , which can be attached to a DataGrid, and does some magic with the DataGrid's columns based on the ViewModel in the DataContext of the DataGrid.
Since the DataContext can be set later, I have to listen for DataContext changes in the behavior. So, I've bound a DependencyProperty to the Associated DataGrid's DataContext, like this:
BindingOperations.SetBinding(this, SourceProperty, new Binding("DataContext") { Source = AssociatedObject });
This line is hit, so the binding does happen.
Now the tricky part:
if I call
datagrid.DataContext = new MyViewModel();
everything works perfectly. But, if the datagrid is contained in some UserControl (not necessarily its immediate child) and I want to call
this.DataContext = new MyViewModel();
the callback of the Source property DOESN'T fire. I debugged it, the datagrid.DataContext is set, so the DataContext is inherited through the visual tree, as it should be, if I manually call update on the behavior, it does see the DataContext, but nothing happens automatically.
I don't want to name the DataGrid instance, I don't want to name the behavior, since there can be any number of those in one UserControl, I want to set the UserControl's DataContext and let the DependencyProperty system work its magic.
What am I doing wrong?
Have you tried something simpler:-
BindingOperations.SetBinding(this, SourceProperty, new Binding());
This should give you the DataContext object. A binding without a Path returns the source object. A binding without an explicit Source returns the current DataContext.
The question is does does the DataContext of this (the behaviour) aquire its value from the DataGrid to which its attached? I think it probably does.

Is there anyway to stop automatic DataContext inheritance in Silverlight?

Is there anyway to stop automatic DataContext inheritance in Silverlight?
I Set my DataContext on my parent UserControl in code. As a result all the xaml bindings inside the UserControl try to bind to the new DataConext they get (through the automatic DataContext Inheritance).
The DataContext's for the children elements (actually they are children of children of children) of the UserControl is something I need to set in the UserControl's code... I don't want them being all smart because they end up binding to the wrong data object! :-)
Can you set the DataContext to {x:Null} in XAML, or null in code, for the items you don't want the inherited context for?

Resources