I'd like to set up a binding in XAML that updates from either the source or target like a TwoWay binding. But when the DataContext is applied, I'd like the initial value to be taken from the Target like a OneWayToSource binding.
<TextBox Text="{Binding Memo}" />
How do I set up this binding so that the TextBox Text is updated from the Memo property of the DataContext, and vice-versa, but that the initial value on hookup is taken from the TextBox text?
Just don't assign a value to the Memo property. By default the value of TextBox.Text is null.
You can set the Binding.TargetNullValue to set a default value:
<TextBox Text="{Binding Memo, TargetNullValue=TextBox default unset value}" />
Related
When the user has set their Keyboard layout to: Chinese(Traditional,Taiwan) Bopomofo as their input language no binding happens as the user inserts text into the TextBox.
The binding only happens after the user loses focus on the textbox.
EDIT: The binding also triggers,once, when all text is deleted from the textbox, binding a empty string to the property.
In the view
<TextBox Text="{Binding ChineseText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
I tried to set xml:lang="zh-Hant" like so, which made no difference:
<TextBox xml:lang="zh-Hant" Text="{Binding ChineseText, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
Note: using English(US) keyboard layout, when the user inputs any az-AZ character the bound property gets updated as expected
EDIT: A regular TextChanged event handler fires whenever i input any character, but still nothing is bound to the property.
I have the need to give the user the ability to set their own Decimal Formating option in XAML.
I know how to set StringFormat into Binding, but I only know how to do it manually. How can I Bind the String Format value inside a binding.
How can I Bind the String Format value inside a binding.
You cannot because StringFormat is not a dependency property.
But you could use a multi binding that binds to two properties, the actual source property and the format source property, and an IMultiValueConverter class:
WPF Binding and Dynamically Assigning StringFormat Property
WPF: MultiBinding and IMultiValueConverter: https://blog.csainty.com/2009/12/wpf-multibinding-and.html
You can use ContentStringFormat property.
URL: https://msdn.microsoft.com/en-us/library/system.windows.controls.contentcontrol.contentstringformat.aspx
Use it like this:
<TextBox Text="{Binding MyFormat, UpdateSourceTrigger=PropertyChanged}" />
<Label Content="{Binding ValueToFormat}"
ContentStringFormat="{Binding MyFormat}" />
I have a RadioButton element whose IsChecked property is bound to MyProperty in ViewModel. The Binding has mode OneWayToSource for some reasons, it pushes the value from RadioButton.IsChecked to ViewModel.MyProperty.
RadioButton.IsChecked is initially false, now. I want to set an initial value from ViewModel, which might be even true. I cannot do that because the property is occupied by the binding.
Is there any way to use Binding with that mode and set default value to the bound property in UI? Something like that:
<RadioButton IsChecked="{Binding MyProperty, Mode=OneWayToSource, DefaultVaule=???}">
</RadioButton>
If I understood you correct, I think this might help:
You can define the default value through the TargetNullValue property. You can define a FallbackValue value in case of error either, for example:
<TextBox Text="{Binding MyProperty, TargetNullValue=0, FallbackValue=10}" />
see here:
enter link description here
I need to update the binding source object with the control values only during a button click event.
But as my top level has datacontext being set, it's updating the source object whenever the control values change... Is it possible to set explicitly to update it during that event only keeping the datacontext as it is?
TextBox binding:
<TextBox x:Name="myTextBox" Text="{Binding MyTextProperty, UpdateSourceTrigger=Explicit}"/>
Button handler code:
myTextBox.GetBindingExpression(TextBox.TextProperty).UpdateSource();
There are two TextBox on the WPF page. While user is typing into the first TextBox, the second TextBox must display modified text from the first TextBox. As a result both values must be binded to a view model.
Can it be done with Data Binding?
I was able to get the second TextBox display text from the first one implementing DependencyProperty on my view model. But I don't have the slightest idea how to apply transformation on the fly.
Maybe there is an easy way to achieve this?
Just use databinding with UpdateSourceTrigger set to PropertyChanged:
<TextBox x:Name='txt1' Text="{Binding MyText, UpdateSourceTrigger=PropertyChanged}" />
<TextBox x:Name='txt2' Text="{Binding MyText, UpdateSourceTrigger=PropertyChanged}" />
In this case it will update ViewModel's property on the fly instead of waiting for FocusLost.