// 对话消息添加进 listBox中 private void AddMessage(string name, string str) { char s = str[0]; StackPanel sp = new StackPanel() { Orientation = Orientation.Vertical, Width = 240 }; switch (s) { case 'T': TextBlock tbName = new TextBlock() { Text = name + ":" }; tbName.Foreground = new SolidColorBrush(Colors.Black); TextBlock tbMsg = new TextBlock() { Text = str.Substring(1), FontSize = 16, TextWrapping = TextWrapping.Wrap }; tbMsg.Foreground = new SolidColorBrush(Colors.LightBlue); if (name == UserName) { sp.HorizontalAlignment = HorizontalAlignment.Right; tbName.HorizontalAlignment = HorizontalAlignment.Right; tbMsg.HorizontalAlignment = HorizontalAlignment.Right; } else { sp.HorizontalAlignment = HorizontalAlignment.Left; tbName.HorizontalAlignment = HorizontalAlignment.Left; tbMsg.HorizontalAlignment = HorizontalAlignment.Left; } sp.Children.Add(tbName); sp.Children.Add(tbMsg); break; case 'E': TextBlock tbNameExpression = new TextBlock() { Text = name + ":" }; tbNameExpression.Foreground = new SolidColorBrush(Colors.Black); MyClass.GifImage myGifImge = new MyClass.GifImage() { Width = 40 }; myGifImge.Source = str.Substring(1); if (name == UserName) { sp.HorizontalAlignment = HorizontalAlignment.Right; tbNameExpression.HorizontalAlignment = HorizontalAlignment.Right; myGifImge.HorizontalAlignment = HorizontalAlignment.Right; } else { sp.HorizontalAlignment = HorizontalAlignment.Left; tbNameExpression.HorizontalAlignment = HorizontalAlignment.Left; myGifImge.HorizontalAlignment = HorizontalAlignment.Left; } sp.Children.Add(tbNameExpression); sp.Children.Add(myGifImge); break; default: TextBlock t = new TextBlock(); t.Text = str; t.Foreground = Brushes.Blue; sp.Children.Add(t); break; } listBoxMessage.Items.Add(sp); listBoxMessage.SelectedIndex = listBoxMessage.Items.Count - 1; listBoxMessage.ScrollIntoView(listBoxMessage.SelectedItem); }