RichTextBox within ItemsContainer or ListBox does not stretch to width of Window
-
Tuesday, March 13, 2012 6:31 PM
I have a ListBox that I want to contain RichTextBoxes. Problem is the RichTextBox does not stretch to the width of the Window. How to do that? Have tried with ListBox and ItemsContainer. Same result.
thanks,
<Window x:Class="Tester.Windows.RichTextBoxWithinListBox" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="RichTextBoxWithinListBox" Height="300" Width="300"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="auto"></ColumnDefinition> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="auto"></RowDefinition> <RowDefinition Height="auto"></RowDefinition> <RowDefinition Height="auto"></RowDefinition> </Grid.RowDefinitions> <ListBox Grid.Row="0" HorizontalAlignment="Stretch"> <ListBox.Items> <RichTextBox> <FlowDocument> <Paragraph> <Run>this is a test</Run> </Paragraph> </FlowDocument> </RichTextBox> </ListBox.Items> </ListBox> <TextBlock Grid.Row="1">Separator line</TextBlock> <ItemsControl Grid.Row="2" HorizontalAlignment="Stretch"> <ItemsControl.Items> <RichTextBox> <FlowDocument> <Paragraph> <Run>this is a test</Run> </Paragraph> </FlowDocument> </RichTextBox> </ItemsControl.Items> </ItemsControl> </Grid> </Window>
All Replies
-
Tuesday, March 13, 2012 6:40 PM
Set HorizontalContentAlignment property to Stretch
<ListBox Grid.Row="0" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch"> <ListBox.Items> <RichTextBox> <FlowDocument> <Paragraph> <Run>this is a test</Run> </Paragraph> </FlowDocument> </RichTextBox> </ListBox.Items> </ListBox>
Or you can try following code
<ListBox Grid.Row="0" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch"> <ListBox.Items> <RichTextBox Width="{Binding Path=ActualWidth, RelativeSource={RelativeSource AncestorType=ListBox}}" > <FlowDocument > <Paragraph> <Run>this is a test</Run> </Paragraph> </FlowDocument> </RichTextBox> </ListBox.Items> </ListBox>
Gaurav Khanna | Microsoft VB.NET MVP
- Edited by Khanna GauravMVP Tuesday, March 13, 2012 6:53 PM
- Marked As Answer by Steve Richter Wednesday, March 14, 2012 3:14 PM
-
Tuesday, March 13, 2012 8:08 PM
Set HorizontalContentAlignment property to Stretch
<ListBox Grid.Row="0" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch"> <ListBox.Items> <RichTextBox> <FlowDocument> <Paragraph> <Run>this is a test</Run> </Paragraph> </FlowDocument> </RichTextBox> </ListBox.Items> </ListBox>
Or you can try following code
<ListBox Grid.Row="0" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch"> <ListBox.Items> <RichTextBox Width="{Binding Path=ActualWidth, RelativeSource={RelativeSource AncestorType=ListBox}}" > <FlowDocument > <Paragraph> <Run>this is a test</Run> </Paragraph> </FlowDocument> </RichTextBox> </ListBox.Items> </ListBox>
Gaurav Khanna | Microsoft VB.NET MVP
Thanks for the reply. The first version does not work for me. And in the 2nd the text does not wrap within the width of the window.
-Steve
-
Wednesday, March 14, 2012 3:22 PM
Thanks for the reply. The first version does not work for me. And in the 2nd the text does not wrap within the width of the window.
-Steve
ok. I was totally confused. Gaurav's solution was perfect. ( I had flipped the width of the containing Grid's column to "auto" from "*", and that was preventing just about everything from working. FWIW, the 3rd version of a RichTextBox in an Items container does not work for me. HorizontalContentAlignment="Stretch"
<Window x:Class="Tester.Windows.RichTextBoxWithinListBox" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:uc="clr-namespace:Tester.Controls" Title="RichTextBoxWithinListBox" Height="300" Width="300"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"></ColumnDefinition> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="auto"></RowDefinition> <RowDefinition Height="auto"></RowDefinition> <RowDefinition Height="auto"></RowDefinition> </Grid.RowDefinitions> <ItemsControl Grid.Row="0"> <ItemsControl.Items> <RichTextBox > <FlowDocument > <Paragraph> <Run>this is a test</Run> </Paragraph> <Paragraph> <Run>this is a test</Run> </Paragraph> </FlowDocument> </RichTextBox> </ItemsControl.Items> </ItemsControl> <ListBox Grid.Row="1"> <ListBox.Items> <RichTextBox Width="{Binding Path=ActualWidth, RelativeSource={RelativeSource AncestorType=ListBox}}" > <FlowDocument > <Paragraph> <Run>this is a test</Run> </Paragraph> </FlowDocument> </RichTextBox> </ListBox.Items> </ListBox> <ListBox Grid.Row="2" HorizontalAlignment="Stretch" HorizontalContentAlignment="Stretch"> <ListBox.Items> <RichTextBox> <FlowDocument > <Paragraph> <Run>this is a test</Run> </Paragraph> </FlowDocument> </RichTextBox> </ListBox.Items> </ListBox> </Grid> </Window>

