wpf - binding stringformat on label using string literal - wpf

I've binded the tooltip of a slider control to it's Value property and i'm trying to use StringFormat to make it display "Current Value {0} of 10" where the {0} is the Value property. Below is one of the various things I tried when trying to figure this out.
<Slider.ToolTip>
<Label>
<Label.Content>
<Binding StringFormat="Current Value {0} of 10"
ElementName="DebugLevelSlider"
Path="Value" />
</Label.Content>
</Label>
</Slider.ToolTip>
I am having issues finding examples online of how to use stringformat with string literals such as mine above. I see a lot of stringformat date/time/currency format conversion. I think I have a way to do this with a multibinding but it just seems like an extra amount of work than necessary. I hope that for string literal formatting I still do not have to write a custom converter.
In my app I find myself using a lot of extra labels next to items so getting an understanding in the stringformat will hopefully let me eliminate some of those unnecessary labels.

Label.Content is object so you can't use Binding.StringFormat there as the binding's target type must be string in order for it to work.
Two work arounds are: use TextBlock instead of Label and bind the Text property.
Use Label.ContentStringFormat i.e.
<Label ContentStringFormat="Current Value {0} of 10" Content={Binding ...} />
You only need to escape the string with {} if your first character is a {

For the ToolTip, you can check out WPF binding with StringFormat doesn't work on ToolTips.
As far as the StringFormat you specified above, you have to escape your string.
StringFormat="{}Current Value {0} of 10"
Here are a bunch of StringFormat examples.
http://blogs.msdn.com/b/llobo/archive/2008/05/19/wpf-3-5-sp1-feature-stringformat.aspx

Related

Cannot bind empty textbox to nullable decimal with MVVM light

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.

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 - Correct Syntax for Using Converter with Current Binding

I have a collection of hex strings that represent colours and I am binding a combobox's ItemsSource to that collection.
The combobox items are templated to have a filled rectangle with the relevant colour. I therefore need to use a converter to convert the hex value to a string. Easy enough.
However, Blend is telling me that this syntax is incorrect in my XAML:
Fill="{Binding, Converter={StaticResource StringToBrush}}"
Apparently, I can't use a converter against plain old 'Binding'. Blend says that something like this is syntactically correct:
Fill="{Binding Value, Converter={StaticResource StringToBrush}}"
...However that obviously doesn't work.
I'm not quite au fait with binding syntax yet, so obviously I'm getting it wrong.
Can anyone advise the correct syntax to achieve what I'm trying to do (convert my bound String using the converter StringToBrush)?
Got it... within about 3 minutes of posting.
I simply didn't need the comma!
The correct syntax is:
Fill="{Binding Converter={StaticResource StringToBrush}}"

wpf string formatting NOT numbers or dates

This should have a simple solution, but I can't seem to find it.
I want to do something like the following, where I have a data binding with a string format. The field is a text field, and I'd like to display it with a suffix (but not change the underlying data).
<Label Name="field" Content="{Binding obj.field, StringFormat=\{0\} suffix}" />
So I want obj.field's value, for instance "value", to display as "value suffix".
Is it really necessary to use a ValueConverter (or whatever) to do this? I'm thinking that if it's possible with the StringFormat construction, then there's some magic format option I just haven't encountered.
This leads to a related question: where can I find a reference for WPF StringFormat? I can find the reference for the c# String.Format formatting options, but these don't all seem to work in WPF (like what I've tried above).
StringFormat will work if the target type is string. However, the type of Label's Content property is object. That is why the StringFormat has no effect. If you put a TextBlock inside the Label (or only use a TextBlock) and bind the Textblock's Text property, it should work fine.
<Label>
<TextBlock Text="{Binding obj.field, StringFormat=\{0\} suffix}" />
</Label>
If you have other reasons to want to bind the value to the Label, you could also do the following.
<Label DataContext="{Binding obj.field}">
<TextBlock Text="{Binding ., StringFormat=\{0\} suffix}" />
</Label>
Related question: I can't think of any reason why normal format strings that you can supply to string.Format() wouldn't work. They all should, both the standard and custom string. Here is a page with multiple examples. If there are any you find are not working, please provide examples.
Found a nice reference here with different examples : http://blog.stevex.net/string-formatting-in-csharp/

How to use Quotation Marks in StringFormat of Binding in WPF

I'm trying to wrap a TextBlock's Text property (which is a string that is pulled from the database) in quotes without using a converter; I'd never tried the Binding's StringFormat property before today, and I seem to be doing something wrong.
Here is the code that isn't working:
<TextBlock Text="{Binding QuoteText, StringFormat='\"{0}\"'}" />
Any ideas?
This is XML, so " is your friend.

Resources