2015-09-05 94 views
3

我正在尝试创建一个像IRC一样的聊天窗口,其中内容从下到上显示,就像任何创建的聊天窗口一样。来自底部的WPF RichTextBox文本

这是我的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> 

,我有一个BackgroundWorker添加文本到它

private void SendWorkerComplete(object s, ProgressChangedEventArgs args) 
{ 
    txtChat.AppendText(args.UserState.ToString()); 
    txtChat.ScrollToEnd(); 
} 

private void SendWorker_DoWork(object sender, DoWorkEventArgs e) 
{ 
    SendWorker.ReportProgress(0, (string)e.Argument); 
} 

的VerticalContentAlignment属性设置为底部不会呈现内容这样,这怎么可能呢?有没有财产或它必须以编程方式完成?

+0

这是一个类似的要求。不完全按照您指定的,但它可能适用于您:http://stackoverflow.com/questions/10308475/how-can-i-make-a-richtextbox-scroll-to-the-end-when-i- add-a-new-line – JamieMeyer

+0

只能滚动到底部,但如果richtextbox是空的,没有用于重现此行为,我想我可以添加一堆空行并滚动结束,然后再将输出附加到它以重现这,但我正在寻找一个更优雅的方式来做到这一点,谢谢 – FuuRe

+0

你能告诉我们你使用的xaml吗? – StillLearnin

回答

-1

你设置保证金左到545云丰富的文本框窗外改变你的代码的东西像这表明你对窗口底部的控制:

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

这并不能解决他的问题。 – StillLearnin

0

为什么用一个RichTextBox麻烦?只需使用常规的TextBox。

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

这是一个聊天,所以它需要有表情符号,不同的字体大小,样式,颜色......你得到想法 – FuuRe

+1

@FuuRe啊!我懂了。我认为你只是使用纯文本界面,因为如果我这样做,并想处理表情符号花式等,我绝对不会使用richtextbox。我首先建立一个包含所有关联属性(发送者,消息等)的消息类,然后构建一个定制的xaml控件来表示消息对象。每次消息到达时,创建一个新的消息对象并将其添加到集合中。将列表视图绑定到该集合,其中列表项模板是您的自定义xaml控件。 – StillLearnin