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" />
Related
There are pictures of what I want to achieve in this question.
I tried putting the TextBlock and the Button in a WrapPanel, and this works for single-line text but not for multi-line text.
<WrapPanel>
<TextBlock
TextTrimming="WordEllipsis"
TextWrapping="WrapWithOverflow" .../>
<Button .../>
</WrapPanel>
The picture below shows what's happening. When the TextBlock text becomes 2 lines the second line takes the whole width of the first line, which is logical.
I thought of chopping the string by words and using an ItemsControl with WrapPanel as ItemsPanel. But the text may become longer, and there isn't space for more than 2 lines, that's why I put the TextTrimming="WordEllipsis" there; the remaining text will be trimmed and the button will be at the end of the second line, in that case.
Update: Well it turned out to be not so easy, I went with changing my UI to something simpler like putting a rounded button on the right.
If you don't mind to use TextBox insteaf of TextBlock, it has TextBox.GetRectFromCharacterIndex method to get the rectangle of character by index number and you can utilize it to adjust the position of Button accordingly.
The following is just a proof of concept and there is much room to modify.
Xaml:
<Grid Width="200" Height="100">
<TextBox x:Name="textBox"
TextWrapping="WrapWithOverflow" AcceptsReturn="True"
TextChanged="TextBox_TextChanged"/>
<Button x:Name="button"
HorizontalAlignment="Left" VerticalAlignment="Top"
Content="END" Padding="0" VerticalContentAlignment="Center"/>
</Grid>
Code behind:
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
AdjustButtonPosition();
}
private void AdjustButtonPosition()
{
if (this.textBox is null || this.button is null)
return;
var index = textBox.Text.Length;
var rect = textBox.GetRectFromCharacterIndex(index);
button.Margin = new Thickness(rect.Right, rect.Top, 0, 0);
button.Height = rect.Height;
}
I am a newbie in WPF trying to create his first normal project which is a notes application.
Every note is linked to a button and I thought it'll look beautiful in StackPanel.
I am dynamically creating buttons within StackPanel, but the problem is that when I scroll down the list of Buttons last Button can't be seen fully. I thought it was because of the Margin and tried to adjust it, but it didn't help, also auto Height and Width didn't help. StackPanel is within ScrollViewer and when I reach the end of ScrollBar I just can see half of last button ?
Here is StackPanel's XAML code:
<ScrollViewer VerticalScrollBarVisibility="Auto"
Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="3" Grid.RowSpan="3" Margin="50 -50 50 5" >
<StackPanel x:Name="ButtonPanel"/>
</ScrollViewer>
That is how I create Button:
dynamicTextBox.Add(SearchNotes);
dynamicTextBox[index_of_buttons] = new TextBox();
Grid.SetRow(dynamicTextBox[index_of_buttons], 1);
Grid.SetColumn(dynamicTextBox[index_of_buttons], 0);
this.ButtonPanel.Children.Add(dynamicTextBox[index_of_buttons]);
dynamicTextBox[index_of_buttons].IsReadOnly = false;
dynamicTextBox[index_of_buttons].Text = "";
Where dynamicTextBox is List<> of TextBoxes to which is applied Template(I think it is not necessary to see Template to resolve the problem)
This is how it looks like:
Can't see the whole button(the last one)
So I wanna see the whole button.
As Andy wrote in the comment section, ListBox is best solution for this problem.
I tried this one to make it look like exactly same as it was in StackPanel
Xaml:
<ListBox Name="ListBoxOfButtons" Background="#404040" BorderThickness="0"
HorizontalContentAlignment="Stretch" Grid.Row="1" Grid.Column="0"
Grid.ColumnSpan="3" Grid.RowSpan="3" Margin="50 -20 50 5">
</ListBox>
C#:
var txtBox = new TextBox();
txtBox.Template = FindResource("TemplateForTextBox") as ControlTemplate;
ListBoxOfButtons.Items.Add(txtBox);
This is how last button looks like now
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);
I have a Customer Pages which contains information of customer name. This page contains a StackPanel and inside this StackPanel there is a ScrollViewer and inside the ScrollViewer there is another StackPanel.
I am adding number of StackPanels dynamically. Inside the StackPanel I am adding TextBlock dynamically behind, which contains text. Then add InkPresenter to show a separation.
My problem is after adding every time when I try to use the page, it doesn't scroll. In fact if I swipe up the page goes down and come back.
XAML :
<StackPanel>
<ScrollViewer Margin="0,-20,0,0" Grid.RowSpan="2" >
<StackPanel Height="Auto" x:Name="pottilelist" >
</StackPanel>
</ScrollViewer>
</StackPanel>
StackPanel stk = new StackPanel();
stk.Name = g.id.ToString();
stk.Tap += new EventHandler<System.Windows.Input.GestureEventArgs>(Customer_Click);
TextBlock tbx = new TextBlock();
tbx.Text = g.customername;
tbx.Name = "A" + g.id.ToString();
tbx.FontSize = 36;
tbx.Tap += new EventHandler<System.Windows.Input.GestureEventArgs>(Customer_Click);
tbx.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
stk.Children.Add(tbx);
InkPresenter ink = new InkPresenter();
ink.Height = 4;
ink.Background = Brush3;
ink.Margin = new Thickness(0, 0, 0, 20);
stk.Children.Add(ink);
pottilelist.Children.Add(stk);
In one app it worked, but in another it doesn't.
Try to put the scrol viewer around the Layout Grid (main grid) that contains all page contents. I think this should solve the problem.
As long as you are using Pivot Template , just put it after the deceleration of that pivot item around the grid and it should works !
<phone:PivotItem Header="PivotItem">
<ScrollViewer>
<Grid x:Name="LayoutRoot" Background="#7F000000" >
It depends on where your stackpanel is placed inside the grid.
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="100"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="1">
<ScrollViewer>
<StackPanel></StackPanel>
</ScrollViewer>
</StackPanel>
</Grid>
In above code scrollviewer will work as expected, because of the height.
If you have set the height of the position in its visual tree, then scrollviewer might not scroll.
I have solved the problem. This problem occurs when there is no height for a stackpanel or scrollviewer. Height Issue
Hi at present I am using a grid with Image and two Buttons for showing a custom message box in my WP7 application whose visibility is collapsed at first. All is working fine but I have to disable all the controls behind on the page when its visibility is visible. So its quite a overhead to enable/disable lots of control behind.
Is there a better solution for my requirement which are :(1) To show a message box having image and two button or textbox and (2) It should appear in the middle of page.
Thanks in advance!!
You can use built in Popup control with an attached behaviour written by Kent Boogaart, so it would behave like WPF Popup control with PlacementTarget and Placement:
<Popup b:PopupPlacement.PlacementTarget="{Binding ElementName=someElement}">
<b:Popup.PreferredOrientations>
<b:PopupOrientationCollection>
<b:PopupOrientation Placement="Top" HorizontalAlignment="Center"/>
<b:PopupOrientation Placement="Bottom" HorizontalAlignment="Center"/>
<b:PopupOrientation Placement="Right" VerticalAlignment="Center"/>
<b:PopupOrientation Placement="Right" VerticalAlignment="TopCenter"/>
</b:PopupOrientationCollection>
</b:Popup.PreferredOrientations>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Grid.Row="0">My popup's contents</TextBlock>
<Image Grid.Row="1" .... />
</Grid>
</Popup>
See the article Silverlight Popup with Target Placement
Download a project
What I do in this situation is to add a Grid or Border to the page that has a transparent background and IsHitTestVisible = True. You can then add your image etc to the parent control (Grid/Border).
You need to make sure the parent control covers the whole page and then just center the dialog inside this control. When you toggle the visibility of the parent control then the transparent background will overlay the other controls on the page, effectively disabling them.
Here is an example. The uxMessageGrid is the parent control and the Border is the actual dialog. You then just need to make sure this is the last control added to the root element and toggle uxMessageGrid.Visibility in your code.
<Grid x:Name="uxLayoutRoot">
<Other Controls />
<Grid x:Name="uxMessageGrid"
Visibility="Collapsed"
Background="Transparent"
IsHitTestVisible="True">
<Border CornerRadius="0"
BorderThickness="1"
VerticalAlignment="Center"
HorizontalAlignment="Center"
BorderBrush="{StaticResource PhoneForegroundBrush}"
Background="{StaticResource PhoneBackgroundBrush}">
<TextBlock Margin="15"
Text="Message..."
TextWrapping="Wrap"/>
</Border>
</Grid>
</Grid>
Use the Custom Dialog box features of the Coding4Fun toolkit
http://coding4fun.codeplex.com/
The toolkit has many controls available beyond the standard Silverlight Toolkit and should more than meet your needs.
Try this one, may be it helps to you
StackPanel st = new StackPanel();
StackPanel st1 = new StackPanel();
Image image = new Image();
image.Height = 300;
image.Width = 300;
image.Source = new BitmapImage(new Uri("/PhoneApp1;component/Koala.jpg", UriKind.Relative));//Build Action=Resource
Button btnok = new Button();
btnok.Content = "Ok";
btnok.Click += new RoutedEventHandler(btnok_Click);
Button btncancel = new Button();
btncancel.Content = "Cancel";
btncancel.Click += new RoutedEventHandler(btncancel_Click);
st1.Orientation = System.Windows.Controls.Orientation.Horizontal;
st1.Children.Add(btnok);
st1.Children.Add(btncancel);
st.Children.Add(image);
st.Children.Add(st1);
ContentPanel.Children.Add(st);