WPF FlowDocument Symmetric Layout - wpf

How can a symmetric layout be generated using one FlowDocument?
My code is:
<Grid Name="grid1">
<FlowDocumentScrollViewer Name="name1" Margin="6,0,17,0">
<FlowDocument>
<Paragraph>
<Figure HorizontalAnchor="PageLeft" VerticalAnchor="PageTop" Width="0.5content">
<BlockUIContainer>
<Button >HelloTwo</Button>
</BlockUIContainer>
</Figure>
<Figure HorizontalAnchor="PageRight" VerticalAnchor="PageTop" Width="0.5content">
<BlockUIContainer>
<Button >HelloTwo</Button>
</BlockUIContainer>
</Figure>
</Paragraph>
</FlowDocument>
</FlowDocumentScrollViewer>
</Grid>
I want them to be aligned side-by-side with each one's content in the same position.
How can I achieve this layout?

You should use FlowDocumentReader or FlowDocumentPageViewer. Using FlowDocumentScrollViewer, Figure is treated as different anchored block, and can't be paired side by side.

Related

Centering several buttons WPF

I was writing C# code in winforms. In winForms using this menu (in the picture) I make center several buttons. But I can not find out any way to make center some elements horizontally in WPF.
Use the HorizontalAlignment property. For example:
<StackPanel Orientation="Horizontal" Height="50" HorizontalAlignment="Center">
<Button Width="50" Margin="5"></Button>
<Button Width="50" Margin="5"></Button>
<Button Width="50" Margin="5"></Button>
<Button Width="50" Margin="5"></Button>
</StackPanel>

How to Set inline Images Vertically Center in RichTextBox

I am working on WPF, i am display RichText data in RichTextBox for that have taken WindowsFormHost, inside that i am taking WinForm RichTextBox to display RichTextData which have Images + Text.
But while display that RichTextData images are align to Top and text are align to Bottom,
See in Image below, red circle is RichTextImage
i want to display images and Text in center. Like Below Image, the Red Circle is RichTextImage that is coming in center with text.
My XAML Code is:
<Window x:Class="WPFRichTextBox.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
Title="MainWindow" Height="600" Width="800" Background="LightBlue" xmlns:my="clr-namespace:WPFRichTextBox">
<Grid Loaded="Grid_Loaded">
<WindowsFormsHost Margin="0,424,0,22">
<wf:RichTextBox Text="RichTextBox" x:Name="richTbTest1" BorderStyle="None" Enabled="True" ForeColor="Black" Width="550" Multiline="True" />
</WindowsFormsHost>
</Grid>
</Window>
I have used WPF RichTextBox also, but in that also i am not able to Align text+Images in Center
<RichTextBox VerticalContentAlignment="Stretch" Height="158" HorizontalAlignment="Left" Margin="10,247,0,0" Name="richTextBox1" VerticalAlignment="Top" Width="754" />
You can use BaselineAlignment on a Run to center align the text. Here is an example:
<RichTextBox>
<FlowDocument>
<Paragraph>
<Run Text="Some text" BaselineAlignment="Center"/>
<Image Height="100" Width="100" Source="Images\Desert.jpg"/>
<Run Text="Some more text" BaselineAlignment="Center"/>
</Paragraph>
<Paragraph/>
<Paragraph>
<Run Text="Paragraph 2" BaselineAlignment="Center"/>
<Image Height="100" Width="100" Source="Images\Desert.jpg"/>
<Run Text="More text" BaselineAlignment="Center"/>
</Paragraph>
</FlowDocument>
</RichTextBox>
EDIT:
To apply the formatting to the entire RichTextBox try calling this method after the RichTextBox is populated:
public void CenterText()
{
var text = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
text.ApplyPropertyValue(Inline.BaselineAlignmentProperty, BaselineAlignment.Center);
}
I was able to get this working with a Span with the BaseAlignment attribute set to "Center".
<RichTextBox>
<FlowDocument>
<Paragraph>
<Span BaseAlignment="Center">
Center My Image
<Image ... />
</Span>
</Paragraph>
</FlowDocument>
</RichTextBox>

Scrolling a FlowDocument inside a TabControl

I have this XAML fragment:
<!-- ... -->
<TabControl>
<TabItem>
<!-- ... -->
</TabItem>
<TabItem Header="Source" ScrollViewer.HorizontalScrollBarVisibility="Visible" ScrollViewer.CanContentScroll="True">
<FlowDocumentScrollViewer>
<FlowDocument>
<Paragraph>
<TextBlock
Text="{Binding Path=CurrentObject.Source}"
FontFamily="Consolas,Courier,Segoe UI"
FontSize="12"
/>
</Paragraph>
</FlowDocument>
</FlowDocumentScrollViewer>
</TabItem>
</TabControl>
<!-- ... -->
The problem is that the flow document does not scroll horizontally. I've been unable to enable that.
Any clues?
Thanks in advance.
There are a couple things here. The first being that using a control in the Paragraph functions differently than a Run that would wrap to fit your FlowDocument.
The second is that the FlowDocument will fit your FlowDocumentScrollViewer. If you add a control to it like you did, it will fit the width of the FlowDocument and viewer and the text will go beyond the TextBlock borders. This means that your document doesn't need a scroll bar; your TextBlock would. You can see this by setting the TextWrapping property of the TextBlock to Wrap.
To get around this, set the PageWidth to something beyond the limits of the viewer width like so:
<FlowDocumentScrollViewer>
<FlowDocument PageHeight="1056"
PageWidth="816">
<Paragraph>
<TextBlock
Text="{Binding Path=CurrentObject.Source}"
FontFamily="Consolas,Courier,Segoe UI"
FontSize="12"
/>
</Paragraph>
</FlowDocument>
</FlowDocumentScrollViewer>
or bind to your TextBlock:
<FlowDocumentScrollViewer>
<FlowDocument PageHeight="1056"
PageWidth="{Binding ElementName=Part, Path=ActualWidth}">
<Paragraph>
<TextBlock
Text="{Binding Path=CurrentObject.Source}"
FontFamily="Consolas,Courier,Segoe UI"
FontSize="12"
/>
</Paragraph>
</FlowDocument>
</FlowDocumentScrollViewer>
The last thing is that the FlowDocumentScrollViewer has it's own HorizontalScrollBarVisibility property that you can use for this (unless some styling issue prevents it).

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>

Align button content

I want the content of a wpf button to be left aligned
I tried the following but the text is still centered inside the button.
<Button >
<StackPanel HorizontalAlignment="Stretch">
<TextBlock HorizontalAlignment="Left" Text="Save"/>
</StackPanel>
</Button>
What do i do?
Found it, it's HorizontalContentAlignment.
:)
You don't need the StackPanel or the TextBlock. All you need is
<Button HorizontalContentAlignment="Left" Content="Save" />
You can align in two way, Horizontal and Vertical
<Button Content="Export" VerticalContentAlignment="Bottom" name="MyBtn1" />
or
<Button Content="Export" HorizontalContentAlignment="Center" name="MyBtn2" />
See the follow image to view the possibilities settings:

Resources