用于wpf应用程序的聊天面板UI

时间:2015-01-18 10:37:01

标签: c# wpf xaml

我正在使用富文本框来显示用户消息。我想要做的是我想在屏幕左侧显示发送者消息和接收者消息。我已经尝试了各种技术,如改变流向,但未能这样做。任何帮助将不胜感激

这是我的xaml:

<wpfTool:RichTextBox>                            
    <FlowDocument>
         <BlockUIContainer></BlockUIContainer>
         <Paragraph>
             <InlineUIContainer></InlineUIContainer>
             <Run Text="{Binding ChatBlock}"  />
             <LineBreak/>
         </Paragraph>
    </FlowDocument>
 </wpfTool:RichTextBox>

2 个答案:

答案 0 :(得分:2)

你必须像这样做一些模板

    <ItemsControl HorizontalAlignment="Stretch"
                  BorderThickness="2"
                  BorderBrush="Red"
                  Grid.ColumnSpan="2"
                  VerticalAlignment="Stretch"
                  Name="ItemsControl1">
        <ItemsControl.ItemTemplate>
            <DataTemplate>
                <RichTextBox HorizontalAlignment="Right"
                             VerticalAlignment="Stretch">
                    <FlowDocument>
                        <Paragraph Name="Para">
                            <Run Text="{Binding Text}" />
                        </Paragraph>
                    </FlowDocument>
                </RichTextBox>
                <DataTemplate.Triggers>
                    <DataTrigger Binding="{Binding Type}" Value="Sender">
                        <Setter TargetName="Para"
                                Property="TextAlignment"
                                Value="Right" />
                    </DataTrigger>
                    <DataTrigger Binding="{Binding Type}"
                                 Value="Receiver">
                        <Setter TargetName="Para"
                                Property="TextAlignment"
                                Value="Left" />
                    </DataTrigger>
                </DataTemplate.Triggers>
            </DataTemplate>
        </ItemsControl.ItemTemplate>
    </ItemsControl>

我创建了一个示例项目here

答案 1 :(得分:0)

基于this帖子;类似下面的内容应该有所帮助

    public static void setRtf(ref RichTextBox rtfBox, string text, bool fromMe = false)
    {
        Paragraph p = new Paragraph();
        p.FontFamily = rtfBox.FontFamily;
        p.TextAlignment = fromMe ? TextAlignment.Left : TextAlignment.Right;
        Run pTxt = new Run();
        pTxt.Text = text;
        p.Inlines.Add(pTxt);
        rtfBox.Blocks.Add(p);
    }