I cannot manage to format double values with leading spaces for labels in XAML.
The leading spaces are necessary for me, because I want labels to display a certain number of characters for the purpose of alignment:
<Viewbox Grid.Column="1" >
<Label xml:space="preserve" Content="{Binding PwVal, FallbackValue='Power'}" ContentStringFormat='#####.0' Foreground="White" HorizontalContentAlignment="Right" VerticalContentAlignment="Center" Margin="0" FontFamily="Consolas">
</Label>
</Viewbox>
The ContentStringFormat='#####.0' does not work. However, '0000.0' gives leading zeros, but what I want is leading spaces.
Use Composite formatting:
<Label ... ContentStringFormat="{}{0,7:0.0} />
Instead of a Label, better use the simpler TextBlock element instead of a Label, and bind its Text property with an appropriate StringFormat:
<TextBlock Text="{Binding PwVal, StringFormat={}{0,7:0.0}}" ... />
I would be inclined to use a TextBlock.
However. Maybe you really need a Label for whatever reason.
When you bind Content of a Label to something then the usual behaviour is that the control puts a textblock in it's contents. It is this which presents the bound data.
A Label is a type of contentcontrol.
An oddity of this behaviour is that it then uses it's ContentStringFormat to format the data goes into that textblock.
Hence you can alternatively do something like:
<Label Content="{Binding PwVal}"
ContentStringFormat="{}{0,6:0.0}"
/>
Note the ContentStringFormat.
I added some markup to prove to myself that I was getting two spaces before my bound value.
<Label Content="{Binding PwVal}"
BorderBrush="Black"
BorderThickness="2"
HorizontalAlignment="Left"
VerticalAlignment="Top"
ContentStringFormat="{}{0,6:0.0}"
/>
Related
I have a Textblock that shows 3 Dots instead of a Minus when Width is set to Auto. The Font is Arial and the FontSize is 20. The Width during Runtime is 7.
<:TextBlock
x:Name="LabelText"
MaxHeight="50"
TextTrimming="CharacterEllipsis"
TextWrapping="Wrap"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Grid.Column="1"
Margin="5,0,0,0"
Style="{StaticResource TextLabelFontStyle}"
Width="Auto" />
Anyone know why i dont see the Minus?
#Bulli, what nit was saying in his comment is that those 3 dots are added to the end of a TextBlock when the text does not completely fit into the allowed space when you use a TextTrimming value of CharacterEllipsis.
So you have two possible solutions... the first is to follow nit's advice and remove that property from your TextBlock. As you said that you don't want to do that, the only other option is to make your TextBlock wider so that the text will fit inside.
This must be some strange TextBlock behaviour. I changed the HorizontalAlignment="Left" to HorizontalAlignment="Stretch" and added TextAlignment="Left". Now it wokrks properly.
Coming from ASP.NET, this WPF stuff is just confusing. All I want to do is put a red asterisk by a label to indicate a required field. Playing around with stuff, I found that this actually does the trick:
<TextBlock Grid.Row="6" Height="28" HorizontalAlignment="Left" VerticalAlignment="Top">
<Label Foreground="Red" Content="*" /><Label Content="Heavy Weight" />
</TextBlock>
Being that I just came up with this, I am not convinced it's the academic route a seasoned WPF developer would take. Additionally, this markup puts a huge amount of white space in between the asterisk and the label. In HTML, a span element would just render right beside its next sibling element. FYI, I tried putting a label within a label, but VS2010 kept barking about "The property 'content' is set more than once".
Any ideas?
Something like this would be more appropriate:
<TextBlock Grid.Row="6" Height="28" HorizontalAlignment="Left" VerticalAlignment="Top">
<Span Foreground="Red">*</Span>Heavy Weight
</TextBlock>
Here is an overview of what can go into a TextBlock's content, more specifically here.
one more way is
<TextBlock Grid.Row="6" Height="28" HorizontalAlignment="Left" VerticalAlignment="Top">
<Run Foreground="Red" Text="*" />
<Run Text="Heavy Weight" />
</TextBlock>
btw
Damascus's solution adds more UI Elements.
with CodeNaked's solution, its difficult to databind the Text.
The explanation is that you actually put two elements one after the other. You need to put them into a container.
Just a sample code of a sentence with red asterisk I did recently:
<StackPanel Orientation="Horizontal" Margin="5" >
<TextBlock Text="Display name"/>
<TextBlock Text="*" Foreground="Red" FontWeight="Bold" />
<TextBlock Text=":"/>
</StackPanel>
There, everything is in a StackPanel, so property 'content' will actually be set once (if you don't specify a group panel such as this one, you'll have to add only one element)
I have bind my Amount Label's Content Property to a decimal property via DataContext. I am trying to apply stringformat but see no effect. Does StringFormat feature work on Label controls ?? Please tell me on which controls does this feature work. BTW following is the code for the Label Control for whom i want to apply the currency formatting
<Label Grid.Column="2" Content="{Binding Path=Amount, StringFormat={}{0:C}}" Height="23" HorizontalAlignment="Left" Margin="100,10,0,0" Name="tb" VerticalAlignment="Bottom" Width="120" />
StringFormat works on properties of type string (when the object you are binding to is being converted to a string the string format is applied). The Content property is of type Object.
You can place a TextBlock inside your label to achieve the desired effect:
<Label Grid.Column="2" Height="23" HorizontalAlignment="Left" Margin="100,10,0,0" Name="tb" VerticalAlignment="Bottom" Width="120">
<TextBlock Text="{Binding Path=Amount, StringFormat={}{0:C}}"/>
</Label>
Try ContentStringFormat
http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/866f7934-8b10-4872-b306-122674fad5fa/
<Label Content=ā{Binding Amount}ā ContentStringFormat=āCā />
This is my current XAML.
<StackPanel Orientation="Horizontal" Grid.Column="3" Grid.Row="1">
<Label Content="Allocated:" FontSize="14"/>
<Label Content="{Binding AllocatedUnits, Mode=OneWay, ValidatesOnDataErrors=True}" ContentStringFormat="N0" FontSize="14"/>
</StackPanel>
How would I change this so that the red validation rectangle is around the whole text instead of just the number. (I will accept throwing away the stack panel entirely and doing something else.
A string-formatted binding would probably do the trick in this case, but that wasn't available in .NET 3.0 (in case you're still using that version!). If you can use it, you'd only need a single label control (so you can ditch both the other label and the stackpanel, and your validation border will wrap all the text in the remaining label).
EDIT: as per Jonathan's comment, it seems you need two attributes to do this on a content control...
Use something like this for your binding:
Content="{Binding AllocatedUnits, ValidatesOnDataErrors=true}" ContentStringFormat="Allocated: {0}"
MSDN documentation is here.
I am attempting to use the WrapPanel and two TextBlocks to append an asterisk (*) to the left side of some text, allow the text to wrap, and force the text to be right aligned. I have successfully done so by creating a WrapPanel with the FlowDirection set to RightToLeft and adding my text, followed by the asterisk. However, if the text I use happens to have any non-alphanumeric characters at the end of the line it is inexplicably forced to the front of the line. I find this behavior to be very strange. I think it must be a bug in WPF and not intended behavior.
Example with Text = Normal Text (Other Text) :
Expected:
* Normal Text (Other
Text)
Actual:
* Normal Text (Other
(Text
Feel free to use the following sample code to recreate the issue for yourself. Simply put this in a window with Height and Width = 100, then type "Normal Text (Other Text)" in the TextBox. Or, set the Height and Width to anything you like and write enough text that it is forced to wrap the text, then add punctuation to the end.
Sample Code:
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBox Name="input" />
<WrapPanel Grid.Row="2" FlowDirection="RightToLeft">
<TextBlock Text="{Binding ElementName=input, Path=Text}" TextWrapping="Wrap"/>
<TextBlock Text="*" Margin="0,0,3,0"/>
</WrapPanel>
</Grid>
So, my question(s).
Is this a bug, or is this intended?
If this is a bug, should I submit it to Microsoft in some way? How?
Since starting this post, I have decided to put the two TextBlocks in a two column grid instead. With the non-asterisk containing TextBlock configured to use a Right TextAlignment I meet all my requirements anyway. Still, I found this to be an interesting issue.
Try this instead:
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBox Name="input" />
<WrapPanel Grid.Row="2" HorizontalAlignment="Right" >
<TextBlock Text="*" Margin="0,0,3,0"/>
<TextBlock Text="{Binding ElementName=input, Path=Text}" TextWrapping="Wrap"/>
</WrapPanel>
</Grid>
FlowDirection is for use to support languages that are read that from right to left. Since I don't know the rules for languages like that I'm not going to pretend to understand why you are seeing what you are, or if it is a bug. That said I know that changing the FlowDirection isn't the correct way to handle right aligning left to right languages and you should use HorizontalAlignment instead.
(For future reference you submit bugs to Microsoft through the Connect site)
In .Net 4 (WPF4) runs are bindable, so you can try something like so:
<TextBlock TextAlignment="Right" TextWrapping="Wrap">
<Run Text="*" /><Run Text="{Binding ElementName=Input, Path=Text}" />
</TextBlock>
The two Run elements are on the same line because any type of whitespace between the tag boundaries will cause a space to appear between the two runs. (As HTML does.)