I'm using xaml-Styler Plugin on VS2015. So far I didn't had any problems.
Now I have the problem, that the styler removes linebreaks (I use HTML encoded characters).
xaml (simplified)
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0}
Min X: {1:F3}; Max X: {2:F3}">
<Binding Path="Area.Name" ... />
<Binding Path="Area.MinX" ... />
<Binding Path="Area.MaxX" ... />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
after Pressing Save, the xaml styler automatically makes this (html character is removed and LineBreak inserted:
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0}
Min X: {1:F3}; Max X: {2:F3}">
<Binding Path="Area.Name" ... />
<Binding Path="Area.MinX" ... />
<Binding Path="Area.MaxX" ... />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
then the text is displayed at one line.
I'm not happy with using html encoded characters at all. Is there maybe a better (simple) way to format strings with linebreaks in a MultiBinding?
For this simple formatting I don't want to use a MultiValueConverter, because it is only an informative string...
If I could tell xaml styler don't style this part/line I would be happy, but didn't found the possibility or a property in options of xaml styler.
You can use the hex representation of the LineFeed character (char 10) :
to get a line break :
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0}
Min X: {1:F3}
Max X: {2:F3}">
<Binding Path="Area.Name" ... />
<Binding Path="Area.MinX" ... />
<Binding Path="Area.MaxX" ... />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
Related
I think this is a super basic question but I can't get it running. I want to show a fixed number inside my WPF View without a binding. This number is 0.001 or in german 0,001. See the seperator.
Now if i switch the UIs language, the number seperator should be updated to the correct one of the language.
<TextBlock>
<Run Text="0.001" />
<Run Text=" " />
...
</TextBlock>
This should be extremly trivial and I think StringFormat should fit the needs, but as I said, i can't get it working. Thanks for your help
Solution: Thanks to #Corentin Pane pointing me to the solution.
As he said, I need to declare the value
<TextBlock>
<TextBlock.Resources>
<system:Double x:Key="MinValue">0.001</system:Double>
</TextBlock.Resources>
<Run Text="{Binding Source={StaticResource MinValue}, Mode=OneTime, StringFormat='N3', ConverterCulture={x:Static gl:CultureInfo.CurrentCulture}}" />
<Run Text=" " />
...
</TextBlock>
If you want WPF to format your number appropriately, it should be a number from the start (like a double) and not a hardcoded string like "0.001". You can use a binding to a static resource:
<TextBlock>
<TextBlock.Resources>
<system:Double x:Key="myFixedValue">0.001</system:Double>
</TextBlock.Resources>
<TextBlock.Text>
<Binding Source="{StaticResource myFixedValue}"/>
</TextBlock.Text>
</TextBlock>
with the following namespace:
xmlns:system="clr-namespace:System;assembly=mscorlib"
Now you can worry about the formatting, and as stated in a comment, this thread provides some hints. For example you can change your Binding to:
<Binding Source="{StaticResource myFixedValue}"
StringFormat="f"
ConverterCulture="{x:Static gl:CultureInfo.CurrentCulture}"/>
and add
xmlns:gl="clr-namespace:System.Globalization;assembly=mscorlib"
declaration.
I had a real-world example of this that outputs
"XXXXX from $0.00 to YYYY" as a tool tip. This is a tad more complex because it shows leading and trailing text and use of a converter with the static/constant number.
Background:
Properties holds the current currency symbol.
CurrencyConverter is a converter that formats the double so it looks
like "$0.00".
ZERO is my constant value.
Code:
<TextBlock>
<TextBlock.Resources>
<system:Double x:Key="ZERO">0.00</system:Double>
</TextBlock.Resources>
XXXXX from
<Run>
<Run.Text>
<MultiBinding Converter="{StaticResource CurrencyConverter}">
<Binding Source="{StaticResource ZERO}" Mode="OneWay"/>
<Binding Source="{x:Static properties:Settings.Default}" Path="CurrencySymbols" Mode="OneWay" />
</MultiBinding>
</Run.Text>
</Run>
to YYYY
</TextBlock>
Overall, I recommend that the accepted answer could be slightly improved by adding the additional Mode="OneWay" to the binding since it is a static number.
I want to bind a string value with string format.
I have tried many ways but it didn't work.
Could anybody help me to solve this problem?
This is the way I am currently using, but I still want to use StringFormat.
How could I do this ?
<DiscreteObjectKeyFrame.Value>
<MultiBinding Converter="{StaticResource DotConverter}">
<Binding Path="LoadingStringShow"/>
<Binding>
<Binding.Source>
<sys:Int16>1</sys:Int16>
</Binding.Source>
</Binding>
</MultiBinding>
</DiscreteObjectKeyFrame.Value>
The best way to format a string is to do this in code.
You could use string.Format() or for C#6's new Feature: "Interpolated Strings" as shown below to format your string.
private string _name;
public string Name
{
get {return $"My Name is {_name}";}
set
{
_name = value;
//OnPropertyChanged("Name");
}
}
Your Binding will then show: My Name is <valueofvariable>
Since your bindings name is LoadingStringShow I assume you want to display some kind of loading message.
Maybe this could also help:
<TextBlock Text="{Binding LoadingStringShow, StringFormat={}{0}%}" />
or
<TextBlock TextAlignment="Center">
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} - {1}%">
<Binding Path="LoadingStringShow" />
<Binding Path="CurrentValue" />
</MultiBinding>
</TextBlock.Text>
Reference 1
Reference 2
I want to make a tooltip with multibinding inside a text block, but whatever I try it doesn't work.
Here is what I've tried so far:
<TextBlock Text="{Binding Description, StringFormat='Description : {0}{}'}">
<ToolTipService.ToolTip>
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="Description : {0}{1}{}">
<Binding Path="FirstDescription" />
<Binding Path="SecondDescription" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</ToolTipService.ToolTip>
</TextBlock>
But when I try it, what I see on the tooltip is : System.Windows.Controls.TextBlock.
when i try it without tooltipservice, and only tooltip, like this :
<TextBlock Text="{Binding Description, StringFormat='Description : {0}{}'}">
<ToolTip>
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="Description : {0}{1}{}">
<Binding Path="FirstDescription" />
<Binding Path="SecondDescription" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</ToolTip>
</TextBlock>
The screen just get stuck.
I dont't know wich VS version you are using but:
<TextBlock Text="{Binding Description, StringFormat="Description : {0}{}"}">
does not even compile for me.
Just remove the " and the empty brackets like that:
<TextBlock Text="{Binding Description, StringFormat=Description : {0}">
You could also write it like this if you want the ":
<TextBlock>
<TextBlock.Text>
<Binding Path="Description" StringFormat="Description : {0}" />
</TextBlock.Text>
<ToolTipService.ToolTip>
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="Description : {0}{1}">
<Binding Path="FirstDescription" />
<Binding Path="SecondDescription" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</ToolTipService.ToolTip>
</TextBlock>
I have tried the following code and that worked perfectly:
<TextBlock Margin="20" Foreground="Black" FontSize="20" FontFamily="Century Gothic" Text="{Binding Name1}">
<TextBlock.ToolTip>
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="MultiBinded Tooltip : {0}{1}">
<Binding Path="Name1"/>
<Binding Path="Name2"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</TextBlock.ToolTip>
</TextBlock>
Just delete empty brackets. Next code work as expected:
<TextBlock Text="{Binding Description, StringFormat='Description : {0}'}">
<ToolTipService.ToolTip>
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="Description : {0}{1}">
<Binding Path="FirstDescription" />
<Binding Path="SecondDescription" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</ToolTipService.ToolTip>
</TextBlock>
If the StringFormat starts with a left brace { the XAML parser require you to escape it using a pair of braces {}. Otherwise the parser gets confused because braces also are used in the syntax of markup extensions.
Details are found in the XAML documentation for {} Escape Sequence / Markup Extension.
Also, you can't use double quotes with inline binding but single quotes is available.
<MultiBinding Converter="{StaticResource MergeValuesConverter}">
<Binding Path="StructureID" />
<Binding Path="SectionID" />
<Binding Path="ParentStructureID" />
</MultiBinding>
I am only getting the first two bindings.
Is there a way to get the third binding (ParentStructureID)?
Is it possible to bind a label content to two values. For eg, I want a single label whose content is displayed as below,
UserName= Firstname, Lastname
where Firstname and Lastname, both are values from database. If I would be using to labels I would bind as Content={Binding Firstname} for one and Content={Binding Lastname}
for another. But I want a single label to display both. Is it possible?
You can do something like this
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0}, {1}">
<Binding Path="firstName" />
<Binding Path="lastName"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>