C# WPF RichTextBox底部的文本

C# WPF RichTextBox底部的文本,c#,wpf,richtextbox,C#,Wpf,Richtextbox,我正在尝试创建一个聊天窗口,比如IRC,其中的内容从下到上显示,就像以前创建的任何聊天窗口一样 这是我的xaml,没什么特别的 <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:ee="http://schemas.microsoft.com/exp

我正在尝试创建一个聊天窗口,比如IRC,其中的内容从下到上显示,就像以前创建的任何聊天窗口一样

这是我的xaml,没什么特别的

<Window
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:ee="http://schemas.microsoft.com/expression/2010/effects" xmlns:ed="http://schemas.microsoft.com/expression/2010/drawing" x:Class="TestChat.Chat"
    Title="Chat" Height="700" Width="400" WindowStyle="ThreeDBorderWindow" ResizeMode="CanMinimize">

    <Grid>
        <RichTextBox x:Name="txtChat" HorizontalAlignment="Left" Height="644" Margin="0,10,0,0" VerticalAlignment="Top" Width="388" VerticalScrollBarVisibility="Auto">
            <FlowDocument />
        </RichTextBox>
    </Grid>
</Window>

设置为“底部”的VerticalContentAlignment属性不会以这种方式呈现内容,如何做到这一点?它是否有属性,或者必须以编程方式完成?

您已将左边距设置为545,这将使富文本框脱离窗口。将代码更改为以下内容将在窗口底部显示控件:

<RichTextBox x:Name="txtChat" HorizontalAlignment="Stretch" Height="42" VerticalAlignment="Bottom" Width="auto" VerticalScrollBarVisibility="Auto" Background="Yellow">
        <FlowDocument />
</RichTextBox>

为什么要为RichTextBox而烦恼?只需使用一个普通的文本框

<Grid>
    <TextBox x:Name="txtChat" VerticalScrollBarVisibility="Auto" Margin="10" Text="Hello" VerticalContentAlignment="Bottom" />
</Grid>


这里有一个类似的要求。不完全像您指定的那样,但它可能对您有效:这只会滚动到底部,但如果richtextbox为空,则无法复制此行为,我想我可以添加一堆空行并滚动到结尾,然后再将输出添加到它以复制此行为,但我正在寻找一种更优雅的方法,谢谢你能给我们看一下你正在使用的xaml吗?当然,这没什么了不起的尽管你有什么理由需要RichTextBox吗?看我的答案。这是一个聊天,所以它需要有表情,不同的字体大小,样式,颜色。。。你得到了idea@FuuRe啊!!我懂了。我假设你只需要一个纯文本界面,因为如果我这样做,并且想要处理表情符号、花哨的样式等等,我肯定不会使用richtextbox。首先,我将构建一个包含所有相关属性(sender、message等)的message类,然后构建一个自定义xaml控件来表示message对象。每次消息到达时,创建一个新的消息对象并将其添加到集合中。使用作为自定义xaml控件的列表项模板将listview绑定到该集合。
<Grid>
    <TextBox x:Name="txtChat" VerticalScrollBarVisibility="Auto" Margin="10" Text="Hello" VerticalContentAlignment="Bottom" />
</Grid>