Inline object in TextBlock with TextTrimming = CharacterEllipsis or WordEllipsis - wpf

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.

Related

How can I center align text with hyperlinks in it?

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" ...

Canvas position based on another element

Basically I want a Canvas to act like a StackPanel. So how do set the Canvas.Top="" based on Canvas.Bottom of another element?
Background: Trying to make an Expander that when expanded it will go over other elements. Figured using a Canvas ZIndex would be the best way to do this. So I created this:
<StackPanel Orientation="Vertical" >
<Canvas Height="{Binding ActualHeight, ElementName=MyExpander}">
<Expander Panel.ZIndex="1" Name="MyExpander" Header="Header" >
<StackPanel Background="LightGray">
<TextBlock Text="Some text" />
<TextBlock Text="Some text" />
<TextBlock Text="Some text" />
<TextBlock Text="Some text" />
<TextBlock Text="Some text" />
<TextBlock Text="Some text" />
</StackPanel>
</Expander>
<StackPanel Panel.ZIndex="0" Canvas.Top="20" Margin="0,5,0,0">
<Button Content="Button1" />
<Button Content="Button2" />
<Button Content="Button3" />
</StackPanel>
</Canvas>
</StackPanel>
Now this works perfectly the problem is that Canvas.Top="20" is hardcoded into the XAML. So that means if the font gets increased (the user increased font sizing in Windows) then part of the StackPanel will be under the Expander. I tried this:
Canvas.Bottom="{Binding (Canvas.Bottom),ElementName=MyExpander}"
The issue being is that value for for Canvas.Bottom for MyExpander is NaN so that isn't going to work.
FYI if there is a better way to do the Expander expands over top of elements I am open to that as well.
thanks
Well this doesn't seem like the greatest answer but it worked for me.
foreach (var exp in MyCanvas.Children)
{
Expander ep = (Expander)exp;
double h = ep.ActualHeight;
Canvas.SetTop(ep, y);
y = y + h + 20;
}
Should note it goes without saying that foreach loop is a bit more complicated due to correct parsing of the objects in the MyCanvas

How to align the bottom edge of multiple TextBlocks with different FontSize

Here is the XAML sample:
<StackPanel Orientation="Horizontal" >
<TextBlock VerticalAlignment="Bottom" Text="Text1" FontSize="20" />
<Image VerticalAlignment="Bottom" ... />
<TextBlock VerticalAlignment="Bottom" Text="Text2" />
</StackPanel>
The result is that the textblocks have different bottom-margins depending on their FontSizes, but I need them all to be on one bottom line with no margins. How to get it? In my case I cant use TextBlock + Runs.
Forget several blobks : the bottom position dépends of the font size.
But, in a single paragraph, write the flow of your objects
You should use something like this :
<RichTextBlock>
<Paragraph>
<Span FontSize="148">Hello</Span>
<InlineUIContainer>
<Image Source="Assets/Logo.png"></Image>
</InlineUIContainer>
<Span FontSize="36">again</Span>
</Paragraph>
</RichTextBlock>

WPF textblock cuts off multiple lines on windows 7

I am using a textblock to display the product description under the product's image. The text must be fixed 100px wide but the height can grow up to 30px tall. If the text still can't fit, it must display ellipsis. Here is my xaml:
<StackPanel>
<!-- I use canvas here to reserve some space for animation (grow/shrink) -->
<Canvas Height="75" Width="75">
<Image x:Name="picture" Height="64" Width="64" .../>
<Canvas/>
<TextBlock Width="100" MaxHeight="30"
TextTrimming="CharacterEllipsis" TextWrapping="Wrap"
Text="{Binding Path=ProductDescription}"
HorizontalAlignment="Center" VerticalAlignment="Top" TextAlignment="Center">
</StackPanel>
The description can have multiple lines. For eg, "Wireless Mouse\nQuantity:20". It looks ok on XP but on Windows 7 only the first line shows up and there's no ellipsis. Can anyone spot the problem in my xaml?
There are several errors in your example: should , ".../>" isn't valid XAML and your TextBlock doesn't have a closing tag.
The follow XAML worked fine on for me on Windows 7:
<StackPanel>
<!-- I use canvas here to reserve some space for animation (grow/shrink) -->
<Canvas Height="75" Width="75">
<Image x:Name="picture" Height="64" Width="64" />
</Canvas>
<TextBlock Width="100" MaxHeight="30"
TextTrimming="CharacterEllipsis" TextWrapping="Wrap"
Text="I use canvas here to reserve some space for animation (grow/shrink)"
HorizontalAlignment="Center"
VerticalAlignment="Top"
TextAlignment="Center" />
</StackPanel>
Depending on the font size MaxHeight of 30 is almost just one line of text, so the textblock can't grow in height. Change it or remove it completely.

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