How to use Quotation Marks in StringFormat of Binding in WPF - 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.

Related

How to bind to a dictionary entry with key ending in '{'?

I want to bind to a value in a dictionary property of an object. The dictionary key of this value is a string ending in '{'. How do I express this in XAML?
I presumably need to escape this character somehow.
Example XAML that doesn't work:
<TextBlock Text="{Binding Attribs[test{]}" />
Here Attribs is a property on the datacontext object of type IDictionary<string, object>
This XAML works, by avoiding using a binding expression and instead using a Binding element:
<TextBlock>
<TextBlock.Text><Binding Path="Attribs[test{]"/></TextBlock.Text>
</TextBlock>
I just have tested the following XAML fragment, and it seems to work fine:
<TextBlock Text="{Binding Attribs[test\{]}"/>
The \ escape character is explained in this article.

Custom DateTime stringformat in WPF

I can't get my custom DateTime string format to work in my binding. I want the format to be "mmmm, yyyy" (e.g. "June, 2012").
The following does not work. I get a short date format (m/d/yyyy).
<TextBlock Text="{Binding ElementName=ThisWindow,
Path=Date,
StringFormat={}{0:MMMM\, yyyy}"/>
I've considered using a converter, but I prefer a pure XAML approach.
Edit:
For clarity, I have a Window with a dependency property Date of type DateTime. In my XAML, I've named the window 'Thiswindow'.
Edit 2:
I looked back at my actual code, and I had a Label, not a TextBlock. I changed it to TextBlock and it works fine.
<Label Content="{Binding ElementName=ThisWindow,
Path=Date,
StringFormat={}{0:MMMM\, yyyy}"/>
Anyone know why it doesn't work with Label?
Thanks.
ContentControls have a ContentStringFormat property which overrides the original formatting.
(When i saw your question i expected this to be the problem actually but was surprised to find a TextBlock at first)
Your month needs to be in uppercase:
{Binding Source={x:Static sys:DateTime.Now}, StringFormat={}{0:MMMM\, yyyy}}
EDIT:
The Label problem is probably because Label has Content, not Text.
Change the Text="{Binding ...}" to Content="{Binding ...}"

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.

wpf - binding stringformat on label using string literal

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

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/

Resources