Binding vs Explicit Assignment of a Variable - c#

I am thinking about the following issue.
If I have for example a textbox/slider/combobox which value is bound to something like
<TextBox Name=textBox Text="{Binding Text}"/>
and then do
textBox.Text = "something"
Is it going to "override" the binding or is binding "stronger" then explicit assignment

No, the binding will be overwritten.

No it won't update your binding. Binding gets updated only when it comes from View, if you set it from your code behind, it will override the text but will break the binding. You can try this sample -
Place a textbox on your view and bind it's text property to some
property in your viewmodel say value for this property is "Test"
Now place two button's on your view.
On first button click simply set the text of your textbox to
something say "Button1".
You will notice that textbox text will now be "Button1" but still the value of your CLR property will be "Test".
Now on second button click, try to set your Viewmodel property to say
"Button2". PropertyChanged will be fired but you won't notice any
change in your textbox text.
If you want to update the binding, you have to set the Dependency property from your code behind like this-
textBox.SetCurrentValue(TextBox.TextProperty, "Button2");
where textBox is your name of the TextBox.

Just simply put the value you will like to be in the textbox in the "Text" memeber of the bound object. Otherwise the binding will be overwritten, as devdigital mentioned above.

Related

How to control two-way binding initial update direction

Every now and then I face a situation where I need to set a two-way binding on a property where it would be preferred that upon setting the binding the initial update will be performed in target-to-source direction and not source-to-target. That is, when the binding is set the source property value is updated so that it reflects the target property value, which stays unchanged.
Is such scenario possible? And if it is, how can it be accomplished?
Of course there are several workarounds, such as caching the target property value, setting the binding and then restoring the cached value, but I'm interested a direct answer rather than a workaround.
Example
Let's say we have a TextBox with Text property set to "foo". Also, we have a view-model with Name property (of type string) set to null. Now what I want to achieve is to bind the Text property to the Name property while preserving the "foo" value. Important thing here is to avoid setting Text to null and then back to "foo" (for whatever reason, e.g. because clearing the TextBox causes other controls to clear as well).
Then the best practice is 1) to read the existing value from the control (Clearly Xaml is not supposed to have data binding). 2) At "Load" event, the control needs to create and establish data binding by calling "SetBinding." 3) Finally get the binding expression for the control and update source with the value from 1).
All the code should be implemented in "View" code, not in "ViewModel."
e.g.) This code snippet is not tested, but has come from my head in the ball park.
private void Loaded(object sender, RoutedEventArg arg)
{
...
var text = textBox.Text;
var binding = new Binding();
... binding property here
textBox.SetBinding(TextBox.TextProperty, binding);
textBox.Text = text;
var expression = BindingExpression.GetBindingExpression(textBox, TextBox.TextProperty);
expression.UpdateSource();
}
How about creating an Attached Property that can be used to any UIElement? This should address the universality requirement.
<TextBlock Text="foo"
GlobalAttachedProperty:Value="{Binding Path=A_ValueFromVM_OR_SomeWhereElse}"
GlobalAttachedProperty:Property="Text"/>
The Value property just contain a callback to set the binding when there is a change. You can add some fancy routine that do this just once.
Then the Property is just a way to get the actual property for binding purposes. There are some other way to accomplish this but this is the more direct way.
I think that the best option is to set a default value in your model class. This is the purpose of the view model.

ValueConverter not called on start up

I have combobox that is bound to a collection. Each item in the collection has a subcollection that might or might not contain items. I have a second combobox to which the subcollection is bound. And I only want to display (=Visibility.Visible) the second combobox if the subcollection has items. All that works. BUT: When I start my program, the selectedItem of the main combobox is null (=so far nothing has been selected). This (I guess) causes the converter to not be called (it doesn't, I have set a breakpoint on the converter and the program does not halt when it starts).
So I have the problem that I don't want to display the second combobox when the program starts. Can I force the ValueConverter to be called? I have tried invoking OnPropertyChanged at the end of the constructor, but that doesn't change anything. And when I set the combobox to Visible.Hidden in the constructor of the .xaml.cs, it will not show at all, even after the value converter is called. Any ideas?
Set a FallbackValue for the binding:
<SomeUIElement Visibility="{Binding SomeBinding, Converter={StaticResource SomeConverter}, FallbackValue=Hidden}" />
When there is nothing bound you can set a value that will be used in place of the binding.
The reason changing the value in code-behind to Visibility.Hidden leaves it invisible all the time is because setting the property explicitly removes the binding that you have added in the XAML

how to invoke convertback of a control that has a converter assigned to it in wpf

how do i invoke convertback method on a control say textbox or button that has been assigned converter in the xaml markup. I need to get the binding value instead of the displayed value. the content property gives the displayed value but i need the original binding value.
You should set BindingMode to TwoWay. This will invoke your ConvertBack method. If you need actual original binding value then my guess would be to create custom attached behavior and remember value with the help of it.

Find and Update a WPF TextBox's Bound Property (From the TextBox)

Say I have a text box like this:
<TextBox Text="{Binding MyBoundProperty, Mode=TwoWay"/>
Is there an easy way to find and set "MyBoundProperty" in code behind?
I am working in a custom attached property for a TextBox (catching the OnTextBoxKeyUp event) and want to set the property directly in some scenarios (when a scan happens).
If it's two-way binding, changing the Text property of the TextBox should update the source.

data binding update when value changes, not when tabbing out

Is there a property or setting to force a bound control in Winforms to update the object it is bound to when the input value, either in a textbox or whatever, actually changes?
And not after the control is tabbed out of.
Assuming you have already bound the textbox to something:
Select the textbox in the designer
Click property "databindings"
Click property "advanced"
In this dialog look for the "datasource update mode"
Select onpropery changed instead of onvalidate
Now the datasource gets updated on every change of the text property of the textbox.

Categories