Global Binding Style - wpf

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.

Related

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;

Inject ViewModel property as ValueConverter

All ValueConverter examples I have found used Resources to create ValueConverter instance. But my ValueConverter uses some dependencies which are resolved by IoC framework. So I would like to set Binding Converter property to some property of my ViewModel which is accessible through DataContext. I tried to do it but got an exception telling I can't bind anything to Converter property of binding.
You cannot bind Binding.Converter as it is not a dependency property, but you can make the converter inherit from DependencyObject and declare dependency properties on that instead, then you can bind those.
Note however that you might need to jump some hoops to get what you want as you will not be able to bind to the DataContext. You probably cannot use ElementName either because the converter will have no name-scope. One common workaround is to target an object with the right DataContext using Binding.Source with x:Reference. Make sure not to declare to instantiate the converter inside the visual tree of the targeted object or x:Reference will throw cyclical dependency errors (the targeted object's Resources are fine).

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!

Binding an Element to a Control Property (string)

so, i've found a way to bind a label to a property on current Control
i give it a name:
<UserControl x:Class="WpfGridtest.GridControl" x:Name="GridControlControl1">
and than bind to property of this control:
<Label Content="{Binding ElementName=GridControlControl1, Path=Filter}"></Label>
I can see the default value i put in that property.
I am guessing that this isn't working because i am binding to String property which doesn't implement INotifyPropertyChanged??
is there some other type i should be using for this property instead of String auto notify my label of changes, or am i going about this the wrong way?
The INotifyPropertyChanged interface should be implemented by the class that contains the property - in this case, by your WpfGridtest.GridControl.
Also, if you want to use your properties for UI, consider using a DependencyProperty as a storage instead of a private field.
in addition, it is also likely that the default binding mode is one time, so you may have to change it in your {Binding}

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