WPF 中的 RichTextBox 无法正确调整内容大小

发布于 2024-08-13 05:05:24 字数 1102 浏览 2 评论 0原文

我需要在 List 中显示带有颜色和格式的文本。我使用 ListBoxRichTextControl 来显示数据。我还需要将内容调整为适合窗口的大小,但文本不需要换行。

当我制作这个简单的示例时,文本显示为垂直,并且不会随着我调整窗口大小而改变。如果我将 RichTextBoxWidth 设置为固定大小(例如 100),那么它就可以工作。

有什么想法吗?

<Window x:Class="WpfApplication19.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
    <Grid>
        <ListBox HorizontalContentAlignment="Stretch">
            <ListBox.Items>
                <RichTextBox>
                    <FlowDocument>
                        <Paragraph>
                            <Run>this is a  test</Run>
                        </Paragraph>
                    </FlowDocument>
                </RichTextBox>
            </ListBox.Items>                
        </ListBox>
    </Grid>
</Window>

如果有更好的选项来显示文本(部分文本颜色不同),请告诉我。

I need to display text with colors and formatting in a List. I'm using a ListBox with a RichTextControl to display the data. I also need the contents to size to the window, but the text does not need to wrap.

When I make this simple example the text appears vertical and doesn't change as I size the window. If I set the Width of the RichTextBox to a fixed size like 100 then it works.

Any ideas?

<Window x:Class="WpfApplication19.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="300" Width="300">
    <Grid>
        <ListBox HorizontalContentAlignment="Stretch">
            <ListBox.Items>
                <RichTextBox>
                    <FlowDocument>
                        <Paragraph>
                            <Run>this is a  test</Run>
                        </Paragraph>
                    </FlowDocument>
                </RichTextBox>
            </ListBox.Items>                
        </ListBox>
    </Grid>
</Window>

If there is a better option for displaying text were parts of the text are different colors please let me know.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

场罚期间 2024-08-20 05:05:24

如果您不需要 ListBox 的列表选择行为,则使用 ItemsControl 提供正确的布局:

<Grid>
    <ItemsControl>
        <RichTextBox>
            <FlowDocument>
                <Paragraph >
                    <Run>this is a  test</Run>
                </Paragraph>
            </FlowDocument>
        </RichTextBox>
    </ItemsControl>
</Grid>

但是要获得您所要求的内容,请包装 RichTextBoxGrid 中,然后绑定到它的 ActualWidth

<Grid>
    <ListBox HorizontalContentAlignment="Stretch">
        <ListBox.Items>
            <Grid>
                <RichTextBox Width="{Binding Path=ActualWidth, RelativeSource={RelativeSource AncestorType=Grid}}" >
                    <FlowDocument>
                        <Paragraph>
                            <Run>this is a  test</Run>
                        </Paragraph>
                    </FlowDocument>
                </RichTextBox>
            </Grid>
        </ListBox.Items>
    </ListBox>
</Grid>

If you don't need the list selection behaviour of the ListBox, then using a ItemsControl provides correct layout:

<Grid>
    <ItemsControl>
        <RichTextBox>
            <FlowDocument>
                <Paragraph >
                    <Run>this is a  test</Run>
                </Paragraph>
            </FlowDocument>
        </RichTextBox>
    </ItemsControl>
</Grid>

The but to get what you asked for, wrap RichTextBox in the Grid and then Bind to it's ActualWidth

<Grid>
    <ListBox HorizontalContentAlignment="Stretch">
        <ListBox.Items>
            <Grid>
                <RichTextBox Width="{Binding Path=ActualWidth, RelativeSource={RelativeSource AncestorType=Grid}}" >
                    <FlowDocument>
                        <Paragraph>
                            <Run>this is a  test</Run>
                        </Paragraph>
                    </FlowDocument>
                </RichTextBox>
            </Grid>
        </ListBox.Items>
    </ListBox>
</Grid>
北城半夏 2024-08-20 05:05:24

这是一个老问题,但可以通过设置 ScrollViewer.Horizo​​ntalScrollBarVisibility="Disabled" 来解决该问题。这与 ListBox 在内部使用 ScrollViewer 以及它与 RichTextBox 的交互方式有关。

<Grid>
    <ListBox HorizontalContentAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
        <ListBox.Items>
            <RichTextBox>
                <FlowDocument>
                    <Paragraph>
                        <Run>this is a  test</Run>
                    </Paragraph>
                </FlowDocument>
            </RichTextBox>
        </ListBox.Items>
    </ListBox>
</Grid>

This is an old question, but the issue can be resolved by setting ScrollViewer.HorizontalScrollBarVisibility="Disabled". This has to do with ListBox using a ScrollViewer internally and how it interacts with RichTextBox.

<Grid>
    <ListBox HorizontalContentAlignment="Stretch" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
        <ListBox.Items>
            <RichTextBox>
                <FlowDocument>
                    <Paragraph>
                        <Run>this is a  test</Run>
                    </Paragraph>
                </FlowDocument>
            </RichTextBox>
        </ListBox.Items>
    </ListBox>
</Grid>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文