How to set ValidatesOnDataErrors from codebehind - wpf

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;

Related

Binding a viewmodel's property to another's

I have a main window coupled with a view model.This main window uses a usercontrol which also has its own viewmodel.
What I would like to achieve is setting a binding in the main window's xaml between one of its viewmodel's custom property and one of the usercontrol's viewmodel's custom property.
How would one go about doing that?
Could you instead use the ViewModels as projections of a Model?
That is, could you have a class that holds the state (or actions) that both the VMs need to expose and have both the VMs reference this class?
If for some reason you have to couple views to something outside their own DataContext I believe you can only go up the visual tree by using RelativeSource FindAncestor in the binding. I don't think you can traverse down (e.g. Window -> Control).
If you really want to Bind them together you could make your ViewModel's properties Dependency Properties and your ViewModel derive from DependencyObject - then you could do..
var binding = new Binding("Something");
binding.Source = myViewModel1;
binding.Mode = BindingMode.TwoWay;
BindingOperations.SetBinding(viewModel2,ViewModelType.SomethingProperty,binding);
If this is a good design having your viewmodels derive from DependencyObject is another question..
You could also try looking at this library that allows binding to and from POCOs.
I ended up not using a modelview for my usercontrol, not as neat but at least it works and is less complicated datacontext wise.
Thanks to all.

TextBox CaretIndex property in ViewModel

Is it possible to get/set the value of CaretIndex property of a TextBox control in viewmodel in wpf via Binding defined in view?
Thanks
You can not bind the property CaretIndex since it is not a DependencyProperty. It is a CLR
property that does not accept binding.
The issue here is how to get the CaretIndex of the TextBox control via the view model.
If you intent to get it directly by binding to the view model its impossible. As I posted in the previous answer its a CLR property and not a dependency property.
What can we do?
The best solution for that is to follow the steps:
Define attached property on the control via separate class.
Define a property in the view model and bind the attached property to the one in the view-model
Update the control property in the callback of the attached property changed event according to the new value received.
In this case, we still separate the view from the model.
I hope my answer helps you!

Dependency Property Set Priority: CodeBehind vs. XAML

When I initialize a control property from code, the binding to the same property defined on XAML don't work. Why?
For Example, I set control properties on startup with this statements:
myControl.SetValue(UIElement.VisibilityProperty, DefaultProp.Visibility);
myControl.SetValue(UIElement.IsEnabledProperty, DefaultProp.IsEnabled);
and on xaml I bind the property of myControl in this way:
IsEnabled="{Binding Path=IsKeyControlEnabled}"
now, when the property "IsKeyControlEnabled" changes to false, myControl remains enabled (because it's initialize with true value).
How can I do?
This is the proper behavior - it is by design. Explicitly assigned values override values obtained through data bindings. WPF bindings remove the need to explicitly reference UI objects and their properties. To set the value of the property simply change the value it is binded to - in your case:
IsKeyControlEnabled = DefaultProp.IsEnabled;

Global Binding Style

I'm wondering how can I set default UpdateSourceTrigger to PropertyChanged for all TextBox'es on window?
Is there any simpler way than using Find&Replace?
UpdateSourceTrigger is used with Binding that is made on some property of the control, so the way you can achieve this is declare your own class derived from Binding, set UpdateSourceTrigger in that class and use this new class in markup instead of Binding class.

Update all bindings in UserControl at once

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.

Resources