Why is AccessText not used when using ContentStringFormat? - wpf

When using a string value as Content of a ContentControl (like Label), using an _ inside the string allows using the following letter as access key, like this:
<Label Content="Text with _access key"/>
or even
<Label Content="{Binding Text}"/>
if Text is a string property containing an _.
However, when using ContentStringFormat which contains the _, it doesn't work any more:
<Label Content="{Binding Value}" ContentStringFormat="_Formatted value {0}"/>
I have seen in the debugger that no AccessText is used in this case.
As a workaround, I have used AccessText explicitly:
<Label>
<AccessText Text="{Binding Value, StringFormat=_Formatted value {0}"/>
</Label>
It works that way but I still want to know why it doesn't when using ContentStringFormat.

Maybe This topic will give you some insight about the AccesText.

Related

Combine text to String.Format

I have this StringFormat StringFormat={}{0:#,0}} that represent decimal numbers and I want add text of my own after this number.
This is what I have tried (but it does not compile with the text inside the StringFormat)
Content="{Binding Path=(my:MyClass.MyStaticProperty),StringFormat={}{0:#,0}} My Text"
my:MyClass.MyStaticProperty is integer type.
As you use Label, Content cannot be formatted with StringFormat. Please view this topic.
Your solution is to use ContentStringFormat:
<Label Content="{Binding Source={x:Static my:MyClass.MyStaticProperty}}"
ContentStringFormat="{}{0:#.0} My Text"/>

Append default string to a label in WPF

I need to display Time inside a Li.e. '20 min'. My data is stored as integer so 'min' should be appended. Is there a way to append a default string 'min' to my bound value?
You can use Run like below
<TextBlock><Run Text="{Binding Time}"/><Run Text=" min"/></TextBlock>
You can simply use the Binding.StringFormat property to format or append additional information to your data bound value:
<TextBlock Text="{Binding Time, StringFormat={}{0} min}" />
When Time has a value of 25, this renders like so:
You can also try using ' marks, but you'll have to leave an initial space:
<TextBlock Text="{Binding Time, StringFormat=' {0} min'}" />
UPDATE >>>
Thanks to #Krishna for the following information:
To use the string format with a Label control, you must use the ContentStringFormat property instead:
<Label Content="{Binding Time}" ContentStringFormat="{}{0} min}" />

Is it possible to conditionally display some text using the StringFormat attribute?

I have a nullable field in the database, called Generation. It specifies things like "Jr.", "II" and so on. I want a mean of conditionally specifying the generation of the client, if it isn't null, otherwise not displaying it at all. I thought that the following would work:
<TextBlock Text="{Binding LastName}" />
<TextBlock Text="{Binding Generation, StringFormat= {0}}" />
<TextBlock Text=", " />
However I'm getting an error saying that "0 is not supported in a Windows Presentation Foundation (WPF) project". The field value Generation is a varchar field. Can I do what I want with the StringFormat attribute of the TextBlock class, or do I need to use a converter?
You need to write this as:
<TextBlock Text="{Binding Generation, StringFormat={}{0}}" />
This is due to the nature of using the Markup Extension, which gives special meaning to {}. By adding this at the beginning (when you have no text before your first format specifier), it has the effect of "escaping" the string format specification for you, similar to how # is used to handle string literals in C#.

Label % suffix in binding

I found something like this:
How to set WPF string format as percent wihout multiplying by 100?
I just want to have a '%' sign suffix after my number. my code looks like this
<Label Content="{Binding Path=ReportGridViewModel.FillingDegree, StringFormat=P}" />
I already tried this one too
<Label Content="{Binding ReportGridViewModel.Cgi, StringFormat={}{0}%}" />
in both cases I don't see any changes like I don't have any stringformat.
The StringFormat property of a Binding is only applied when the target property is of type String. In a Label, the target property Content, is of type Object, so StringFormat is not respected.
To get this to work in a label, use ContentStringFormat. If you were to use a TextBlock, you could use the StringFormat provided by Binding.
In the case of Label you need to use the ContentStringFormat property
<Label Content="{Binding Path=ReportGridViewModel.FillingDegree}"
ContentStringFormat="P" />

How can I add bold text and AccessText to a Label or TextBlock?

I have a WPF conundrum. I want some text to look like this:
Enter this preparer's info:
[ComboBox]
Alt+E is the access key that focuses the ComboBox, and when Alt is pressed, the E in the text should be underlined.
I can get the access key to work easily:
<Label Target="{Binding ElementName=PreparerComboBox}">
_Enter this preparer's info:</Label>
But then "preparer's" can't be bold because a Label doesn't support Runs (as far as I can tell).
I can do the bolding easily in a TextBlock:
<TextBlock>Enter this <Bold>preparer's</Bold> info:</TextBlock>
But there's no access key defined, so I tried adding my AccessText inside the TextBlock:
<Label Target="{Binding ElementName=PreparerComboBox}">
<TextBlock>
<AccessText>_Enter</AccessText> this <Bold>preparer's</Bold> info:
</TextBlock>
</Label>
But then the AccessText doesn't line up properly with the rest of the text in the TextBlock, and Margin doesn't seem to have any effect on it.
Example:
The best I've come up with so far is this monstrosity:
<Label Target="{Binding ElementName=PreparerComboBox}">
<WrapPanel>
<AccessText>_E</AccessText>
<TextBlock>nter this <Bold>preparer's</Bold> info:</TextBlock>
</WrapPanel>
</Label>
What am I missing here? Seems like there has to be an easier way.
Didn't change much but how about
<Label Target="{Binding ElementName=PreparerComboBox}">
<StackPanel Orientation="Horizontal">
<AccessText>_Enter</AccessText>
<TextBlock xml:space="preserve"> this <Bold>preparer's</Bold> info:</TextBlock>
</StackPanel>
</Label>

Resources