How can I center align text with hyperlinks in it? - wpf

How can I center align text with hyperlinks in it?
I need to have the some text below, and with the XAML I got it's currently not centered. In fact, Horizontal Alignment does not seem to have any effect. Hyperlinks are working fine, however.
This is inside a grid with 2 columns, and I need the ColumnSpan to be 2.
Thanks.
"By clicking Sign In, you agree to our [HYPERLINK: Privacy Policy] and [HYPERLINK: Terms of Use.]"
<TextBlock Grid.Row="3" Grid.ColumnSpan="2" TextWrapping="Wrap" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="White" FontSize="18px">
<Run Text="By clicking Sign In, you agree to our " />
<Hyperlink NavigateUri="https://www.my-company-website.com/privacy-policy/" RequestNavigate="Hyperlink_RequestNavigate">
Privacy Policy
</Hyperlink>
<Run Text=" and "/>
<Hyperlink NavigateUri="https://www.my-company-website.com/terms-and-conditions/" RequestNavigate="Hyperlink_RequestNavigate">
Terms of Use.
</Hyperlink>
</TextBlock>

I think you are looking for the TextAlignment property. Set this one to Center:
<TextBlock TextAlignment="Center" ...

Related

Grouping labels and hyperlink button together in silverlight

I have a label and a hyperlink button in silverlight and I would like to group them together so that the alignment is done on that group rather that having to align two different elements. Any idea of how this could be done please?
<TextBlock Text="Hello" Foreground="Black" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="0,0,177,6"/>
<HyperlinkButton NavigateUri="http://www.mywebsite.mt/" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="0,0,132,6">
<TextBlock Text="myWebsite" Foreground="Black" TextDecorations="Underline" />
</HyperlinkButton>
I want these a group, so I could only use 1 margin alignment
You shouldn't really need to use Margin like that, but since it looks they would just sit on top of one another you could of course use something like StackPanel to accomplish it.
<StackPanel HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="0,0,0,6">
<TextBlock Text="Hello"/>
<HyperlinkButton Content="myWebsite" Foreground="Black" NavigateUri="http://www.mywebsite.mt/"/>
</StackPanel>
Hope this helps.

Inline object in TextBlock with TextTrimming = CharacterEllipsis or WordEllipsis

If you run this code and minimize/maximize width of the window
<TextBlock TextTrimming="WordEllipsis" >
<Run Text="I want that this rectangle will be placed "/>
<Rectangle Fill="Black" Width="20" Height="10" />
<Run Text=" here when I minimize width of the window"/>
</TextBlock>
you will see what Rectange will shift to the left side.
Is a bug in the WPF?
I guess this is not a bug.
Try to take TextTrimming="WordEllipsis" property off the text block (it affects the whole control)
and you will see that the rectangle wont move with you window size changes beacause you dont have any HorizentalAligment properties.
While it seems to be a bug, this might be a workaround:
<TextBlock TextTrimming="WordEllipsis" >
<Run Text="I want that this rectangle will be placed "/>
<Run Text="■" FontSize="40" BaselineAlignment="Center"/>
<Run Text=" here when I minimize width of the window"/>
</TextBlock>
See Unicode Characters in the Geometric Shapes Block.

WPF: Is there a sort of XAML tag that behaves like the HTML span element?

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)

WPF: Wrapping multi-part text

Currently I'm using a TextBlock to show a single line with an image.
<TextBlock>
<Image Name="StatusImage" Stretch="Fill" MaxWidth="12" MaxHeight="12"
Source="/Aam.Cerberus.Applications;component/Images/Warning.png"></Image>
<TextBlock Text="{Binding Path=ServiceStatusText}"></TextBlock>
<TextBlock Text=" ("></TextBlock>
<TextBlock Text="{Binding Path=ServiceMachineName}"></TextBlock>
<TextBlock Text=")"></TextBlock>
</TextBlock>
My questions are:
Is a TextBlock the right way to do this sort of thing?
How do I enable word wrapping?
You want the TextWrapping="Wrap" property.
However, according to the MSDN
TextBlock is not optimized for scenarios that need to display more than a few lines of content; for such scenarios, a FlowDocument coupled with an appropriate viewing control is a better choice than TextBlock, in terms of performance.

Set superscript and subscript in formatted text in wpf

How can I set some text as subscript/superscript in FormattedText in WPF?
You use Typography.Variants:
<TextBlock>
<Run>Normal Text</Run>
<Run Typography.Variants="Superscript">Superscript Text</Run>
<Run Typography.Variants="Subscript">Subscript Text</Run>
</TextBlock>
You can use something like <TextBlock>5x<Run BaselineAlignment="Superscript">4</Run> + 4</TextBlock>.
However, as far as I know, you will have to reduce the font-size yourself.
It's interesting to note that for some characters (m2, m3, etc) a superscript is not needed, but the unicode character can be used. For example:
<Run Text=" m³" />
This would show m3.
I used a layout transform, because Typography.Variants often doesn't work:
<TextBlock Text="MyAmazingProduct"/>
<TextBlock Text="TM">
<TextBlock.LayoutTransform>
<!-- Typography.Variants="Superscript" didn't work -->
<TransformGroup>
<ScaleTransform ScaleX=".75" ScaleY=".75"/>
<TranslateTransform Y="-5"/>
</TransformGroup>
</TextBlock.LayoutTransform>
</TextBlock>
<TextBlock Text="{Binding Path=Version, StringFormat={} v{0}}"/>
The advantage of using a LayoutTransform is that it is insensitive to the fontsize. If the fontsize is changed afterwards, this superscript works where explicit FontSize setting breaks.
I don't know if you need this to work with FormattedText specifically, or you mean derivations of Inline, but the following will work on Inlines, even if Typography.Variants="Superscript" fails to work.
TextRange selection = new TextRange(document.ContentStart, document.ContentEnd);
selection.ApplyPropertyValue(Inline.BaselineAlignmentProperty, BaselineAlignment.Superscript);
Hope it helps!
Typography.Variants works only for open type fonts. If you dont like your superscripts/subscripts going outside the height of actual text then you can use something like the following:
<StackPanel Orientation="Horizontal">
<TextBlock FontSize="10" Margin="0,5,0,0">1</TextBlock>
<TextBlock FontSize="30">H</TextBlock>
<TextBlock FontSize="10" Margin="0,20,0,0">2</TextBlock>
</StackPanel>
This is the only thing that worked for me. It also gives you more control over the alignment and font size.
<TextBlock Grid.Row="17">
3 x 3<Run FontSize="6pt" BaselineAlignment="TextTop">2</Run>)
</TextBlock>
Setting for superscript works fine with the following code:
<TextBlock Text="(cm" />
<TextBlock ><Span BaselineAlignment="Top" FontSize="8">2</Span></TextBlock>
<TextBlock Text=")" />
Setting the Baseallignment for subscript in the Span tag did not work for me.
I tried the following code and it worked fine.
<TextBlock Text="H" />
<TextBlock Text="2" Margin="-2,0,-2,0" TextBlock.LineHeight="3" >
<TextBlock Text="O" />

Resources