C# windows phone 8 app paragraph - c#

Hello I am new to Windows Phone app development. I am right now developing an app that shows a paragraph of text. I used a scroll viewer in order to show the text. I have put the entire paragraph in to a single text block. the problem is when I run the app the emulator displays only half of the text and the remaining half is not visible how to make all the text inside the paragraph visible??. Thank you

Try setting
TextWrapping="Wrap" on TextBlock
Also make sure scrollviewer size is not set to Auto as it will keep expanding to accommodate the text. Try to give screen width size.

Use:
<ScrollViewer>
<TextBlock Text="Your paragraph here" TextWrapping="Wrap" />
</ScrollViewer>

Hi you can use RichTextBox for it like:
<RichTextBox FontSize="25" Background="Transparent">
<Paragraph>
your text goes here.
</Paragraph>
</RichTextBox>

Just add below code.
XAML Code :
<ScrollViewer Grid.Row="1">
<StackPanel>
<RichTextBox x:Name="richTxtBox" VerticalAlignment="Top" Style="{StaticResource NormalRichTextBoxStyle}"/>
</StackPanel>
</ScrollViewer>
Class Code :-
string str = "***** Your String *****";
Paragraph p = new Paragraph();
Run myRun = new Run();
myRun.Text = str;
p.Inlines.Add(myRun);
richTxtBox.Blocks.Add(p);

Related

How to show a flyout in where the text in a TextBlock is selected

I want to add a flyout to my TextBlock, and when I select the text in the TextBlock, the flyout will show up at the selected (kind of like Reading Mode in Microsoft Edge, when you select the text in reading mode, there will be a flyout show the word's definition). But I don't know how. I've tried using the SelectionChanged, but the parameters that this event passes don't have an position that I can use to set the flyout. So how can I do that?
Besides, I'm wondering what SelectionFlyout is for? I thought it could help me.
Here was my code:
<TextBlock x:Name="webviewtest" Grid.Row="1" Text="This is a select-flyout test." FontSize="300" IsTextSelectionEnabled="true" >
<TextBlock.SelectionFlyout>
<Flyout>
<TextBlock Text="this is the flyout"></TextBlock>
</Flyout>
</TextBlock.SelectionFlyout>
</TextBlock>
And when I selected text, the flyout never showed up. It's obviously that I have been using it wrong. So I checked the Microsoft Docs and it said
Gets or sets the flyout that is shown when text is selected, or null if no flyout is shown.
And I can't find any samples about this online.
This is achievable by setting the TextBlock IsTextSelectionEnabled to True and by using a MenuFlyout to display the selected text.
XAML
<TextBlock x:Name="webviewtest" Text="This is a select-flyout test." FontSize="100" IsTextSelectionEnabled="True" RightTapped="webviewtest_RightTapped">
<FlyoutBase.AttachedFlyout>
<MenuFlyout x:Name="Flyout">
<MenuFlyout.Items>
<MenuFlyoutItem x:Name="FlyItem" Text="">
</MenuFlyoutItem>
</MenuFlyout.Items>
</MenuFlyout>
</FlyoutBase.AttachedFlyout>
</TextBlock>
C#
private void webviewtest_RightTapped(object sender, RightTappedRoutedEventArgs e)
{
TextBlock tb = sender as TextBlock;
if (tb.SelectedText.Length > 0)
{
Item.Text = tb.SelectedText;
}
// Show at cursor position
Flyout.ShowAt(sender as UIElement, e.GetPosition(sender as UIElement));
}
You need to use RichTextBlock to replace TextBlock and the platform is 17134 and laster.
<RichTextBlock HorizontalAlignment="Center"
VerticalAlignment="Center"
IsTextSelectionEnabled="True">
<RichTextBlock.ContextFlyout>
<Flyout>
<TextBlock Text="flyout" />
</Flyout>
</RichTextBlock.ContextFlyout>
<RichTextBlock.SelectionFlyout>
<Flyout>
<TextBlock Text="this is the flyout" />
</Flyout>
</RichTextBlock.SelectionFlyout>
<Paragraph>
welcome to blog.lindexi.com that has many blogs
</Paragraph>
</RichTextBlock>
The SelectionFlyout is work in touch. TextBlock.SelectionFlyout doesn't work · Issue #452 · Microsoft/microsoft-ui-xaml
All the code in github

Mahapps Metro richtextbox cuts bottom of g letter

My problem with the RichTextBox control is that it cuts off the bottom part of 'g', 'q', and 'j' letters of the last Paragraph added to it. My application is a chat, so it adds one paragraph per message to the FlowDocument of this RichTextBox and it scrolls down.
I think this is not the problem of Paragraph because this happens only with the last Paragraph. When a new one is added and the old is scrolled upper then it doesn't have this problem.
The XAML code looks like this:
<Border Grid.Row="0" BorderThickness="0,1" BorderBrush="Gray">
<ScrollViewer Margin="0,5">
<RichTextBox IsUndoEnabled="False" IsReadOnly="True" BorderThickness="0" IsDocumentEnabled="True" Background="Transparent">
<FlowDocument />
</RichTextBox>
</ScrollViewer>
</Border>
And I add a new Paragraph with this C# code:
Paragraph p = new Paragraph();
p.Margin = new Thickness(0, 2, 0, 2);
p.Inlines.Add(new Run(msg.Text));
rtbDocument.Blocks.Add(p);
I hope that somebody knows a clever solution for this problem, thanks in advance!
Ps.: I don't know the exact name of this problem, so I would be happy if somebody would modify the title of this question to a proper one :)
I think it is related to line stacking.
Line stacking strategy on MSDN.
I used following approach without problems :
<Border Grid.Row="1" BorderThickness="0,1" BorderBrush="Gray" Grid.RowSpan="2">
<RichTextBox x:Name="rtbDocument" ScrollViewer.VerticalScrollBarVisibility="Auto" IsUndoEnabled="False" IsReadOnly="True" BorderThickness="5" IsDocumentEnabled="True" Background="Transparent" Margin="0,34,0,69">
<FlowDocument/>
</RichTextBox>
</Border>
Paragraph p = new Paragraph();
p.FontSize = 20;
p.Margin = new Thickness(0, 5, 0, 5);
p.Inlines.Add(new Run("some gqgb qqj"));
rtbDocument.Document.Blocks.Add(p);
//
Your problem is reproduced if I do following changes to FlowDocument in your RichTextBox :
<FlowDocument LineHeight="20" LineStackingStrategy="BlockLineHeight" />
or, to your code like :
Paragraph p = new Paragraph();
p.LineHeight = 20;
p.LineStackingStrategy = LineStackingStrategy.BlockLineHeight;
or,
<FlowDocument LineStackingStrategy="BlockLineHeight" />
p.LineHeight = 20;
See figure below :
Removing LineStackingStrategy altogether will work as it will use default value of MaxHeight. Or, you can set it explicitly :
<FlowDocument LineStackingStrategy="MaxHeight" />

WPF max width of TextBlock in C#

I have need to programmatically create a TextBlock inside a WrapPanel. This TextBlock will act like a heading, therefore I don't want anything to appear to right of the TextBlock. Is there a better way to max out the Width of the TextBlock without doing something like;
myTexblock.Width = 1000000;
Thanks
I think a better solution than putting your header in your WrapPanel is to place the header and WrapPanel in a StackPanel
Something similar to this:
<StackPanel>
<TextBlock Text="Some Header Text"
HorizontalAlignment="Stretch" />
<WrapPanel>
<Button Content="Placeholder" />
<Button Content="Also holding a place" />
</WrapPanel>
</StackPanel>
This gives the same visual effect as what you described without the sloppy property setting.

How to "float" image in XAML (Windows 8 Metro)

Is it possible to do in XAML something like ffloat:left for an image in CSS. I need to create something like this:
with variable image dimensions and text length.
Edit: The text warp around the image is not static in my case, it is create in C# code that returns a list of TextBlock elements (with Runs)
With Silverlight 4 you would achieve this using a RichTextBox:
<RichTextBox TextWrapping="Wrap" IsReadOnly="False">
<Paragraph>
More text here ..
<InlineUIContainer>
<Image Source="abc.jpg"/>
</InlineUIContainer>
more and more text here;
<LineBreak />
</Paragraph>
</RichTextBox>
It looks like Win8 Metro has a RichTextBox, and also has an InlineUIContainer, so something like the above should work!
The solution seems to be using RichTextBlockOverflow and OverflowContentTarget described in this presentation: http://video.ch9.ms/build/2011/slides/APP-914T_Street.pptx
This question seems to be asking for something similar to what you want. The answer here should prove a solution to what you desire.
A summary of the answer is, use a FlowDocument like the following example:
<FlowDocument>
<Paragraph>
<Floater HorizontalAlignment="Left">
<BlockUIContainer>
<Image Source="/FlowDocumentTest;component/dog.png" Width="100" />
</BlockUIContainer>
</Floater>
Here is where the text goes to wrap around the image.
</Paragraph>
</FlowDocument>
Update
As your question states, you are now using some C# code to generate TextBlock/Run Elements, both can be children of a Paragraph object. So simply name your Paragraph like so:
<FlowDocument>
<Paragraph x:Name="textPara">
<Floater HorizontalAlignment="Left">
<BlockUIContainer>
<Image Source="/FlowDocumentTest;component/dog.png" Width="100" />
</BlockUIContainer>
</Floater>
</Paragraph>
</FlowDocument>
Then in C#, add your generated TextBlocks or Runs to the Inlines property of textPara, i.e.
var runToInsert = new Run("Some text to display");
textPara.Inlines.InsertAfter(textPara.Inlines.FirstInline, runToInsert);

List box in c# windows phone 7

Does list box allow placing a paragraph of word into it instead of a item one after another?
I think you just need to put a text block inside a scroll viewer, but this article shows how to make a control:
See this article
This is some code that does the basic work:
<ScrollViewer x:Name="MyScroller">
<TextBlock x:Name="MyTextBlock" TextWrapping="Wrap" Text="Some very long text here..." />
</ScrollViewer>
You'll see that if the area containing the text is bigger than the size of the textblock, you'll be able to scroll to view all text.

Categories