Cannot bind empty textbox to nullable decimal with MVVM light - wpf

Is there any solution to binding a textbox to a nullable decimal? Whenever you leave the textbox empty the binding is not updated. I know one solution is to use a string property instead but I really don't want to do it that way.

You can do the trick by using TargetNullValue.
<TextBox Text="{Binding Value, TargetNullValue=''}"/>

If simple bindings won't do you can always use a converter.

Related

Is it possible to use binding inside other binding?

Is it possible to write something like this
<TextBlock Text="{Binding Path=TextSource, StringFormat='{Binding Path=StringFormat}' }"
Or the single way is to have three properties: one for some value and other for string presentation of this value, third for format string. In this case TextBox binds with string representation of value. String presentation changes when format string changes.
Yes, it is possible in general and no for your case it is not possible because StringFormat is not Dependency Property.
Binding only works on Dependency Properties.
If you wish that to work create a resource dictionary of type Freezable and let it inherit the actual DataContext. Futhermore use StaticResource extension to set StringFormat in Binding.
StringFormat is not DependencyProperty but it doest accept {StaticResource someKey}.
It's a workaround. But it would work.
Another alternative solution would be attached property.
Attached properties are bindable. You would need to listen to property changed event of your attached property and change the StringFormat inside the handler.

Xaml: Itemssource binding + fallbackValue

I've got a Listpicker with a DataBinding on the Itemssource-Property. Binding works fine. Now I want to define a FallbackValue. My problem is, that the FallbackValue is interpreted as a list: {'S','t','a','n','d','a','r','d'}, not as a single item 'Standard'. I'm looking for a solution to solve this problem. Any idea?
<toolkit:ListPicker x:Name="listPicker" ExpansionMode="FullScreenOnly" ItemsSource="{Binding Profilelist, ElementName=userControl, FallbackValue='Standard'}" SelectedIndex="0" />
The fallback behaviour is correct as the target expects an array (and a string an usable as an array of chars). There is no easy way to specify an array for the fallback.
I would suggest binding to a ViewModel list, instead of directly to the other control, so you can specify whatever default you want in the list. It does mean an extra binding and a property on your ViewModel (or code-behind... yuk) but element binding is not designed to have a fallback array, only single values.
If you can provide more code/Xaml I will be able to be more specific.

WPF: Textbox Binding with StringFormat={}{0:F2}. Don't show zero's

I am binding an object to a TextBox with the following XAML:
<TextBox Name="MyTextBox" Text="{Binding Path=MyValue, Mode=TwoWay, StringFormat={}{0:F2}}" />
Naturally when I bind a new object (which values are all still zero) the Text property is set to 0.00. I have several of these TextBoxes, which makes it tedious to delete every value before entering a new one.
At the moment I'm clearing these boxes in the Window_Loaded method using the FindVisualChildren method.
It just feels clunky though. Is there a neat way of doing this?
Try the following:
StringFormat={}{0:#.##}
It will format to two decimal places and won't show zeroes.

Is there any way to set binding for the StringFormat as well?

<TextBox Text="{Binding Path=Double, StringFormat=F3}"/>
StringFormat in the below statement is hard coded. But it will change according to the culture and Customized settings. How can we se the rounding or String format using Dependency property or normal property? This can be applied to TextBox, Label , TextBlock etc where ever we wish to use the String formatting.
No, you can't bind StringFormat, since it's not a dependency property. When you doing something more serious than basic formatting consider two options:
Option 1. Make your ViewModel (i.e. binding source) return data in a most convenient way.
Option 2. Consider using custom value converters.

WPF textblock binding question

I'm trying to get my head around the whole MVVM thing and binding. I have a ViewModel class which has a property that is another class. I want to bind to a (string) property of that class to the text of a textblock.
I set the ViewModel as my data context for my window\page. And then do this:
<TextBlock Text="{Binding ElementName=myAddressClass, Path=StreetName}" />
But this does not work. The text is empty.
I can expose the StreetName directly as below and this works:
<TextBlock Text="{Binding Path=StreetName}" />
So am I doing something wrong in the first example. It seems simple enough ... am I just confuse about what an elementname is or should be set to?
thanks
I think you probably are confused. If you want to bind to MyAddress.StreetName, just do this: Text="{Binding MyAddress.StreetName}" Make sure MyAddress is a property of your DataContext. ElementName is for binding to other controls.
ElementName is used to reference a XAML element in the Logical Tree. Since what you're trying to bind to is not an element, ElementName isn't the correct approach. Dotted path notation is the simplest approach in this case:
{Binding Path=myAddressClass.StreetName}

Resources