如何在WPF中创建这样的东西?我可以显示文本消息的框,较旧的框移动到顶部而不是消失。像聊天一样的东西。 使用滚动条并不需要花哨,我只需要一个能够显示多条短信的消息框。创建消息的时间非常方便,但不是必需的。
答案 0 :(得分:2)
您可以将窗口用作对话框,并将消息绑定到该对话框的列表视图。我做了示例应用程序来向您展示如何做到这一点。这只是向您展示非常基本的消息传递应用程序的示例。不要在生产中使用它。
MainWindow示例:
<Window x:Class="Messaging.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Messaging"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Grid>
<StackPanel Orientation="Horizontal" VerticalAlignment="Center" HorizontalAlignment="Center">
<TextBox Name="txtMessage" Width="200" Height="50" Margin="10" />
<Button Name="btnSubmit" Content="submit" Width="200" Height="50" Click="btnSubmit_Click" />
</StackPanel>
</Grid>
</Window>
代码背后。
using System;
using System.Windows;
namespace Messaging
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
MessageStorage.Messages = new System.Collections.ObjectModel.ObservableCollection<Message>();
}
private void btnSubmit_Click(object sender, RoutedEventArgs e)
{
Message message = new Message();
message.MessageContent = txtMessage.Text;
message.PublishDate = DateTime.Now;
MessageStorage.Messages.Add(message);
MessageDialog messageDialog = new MessageDialog(MessageStorage.Messages);
messageDialog.ShowDialog();
}
}
}
消息对话框wiew:
<Window x:Class="Messaging.MessageDialog"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:Messaging"
mc:Ignorable="d"
Title="MessageDialog" Height="300" Width="300">
<Grid>
<ListView Name="lvMessages">
<ListView.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding MessageContent}" Margin="0,10,10,0" />
<TextBlock Text="{Binding PublishDate}" Margin="0,10,10,0" />
</StackPanel>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
</Window>
背后的消息对话代码:
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
namespace Messaging
{
/// <summary>
/// Interaction logic for MessageDialog.xaml
/// </summary>
public partial class MessageDialog : Window
{
public MessageDialog(ObservableCollection<Message> messages)
{
InitializeComponent();
lvMessages.ItemsSource = messages.OrderByDescending(m => m.PublishDate);
}
}
}
MessageStorage类:
using System.Collections.ObjectModel;
namespace Messaging
{
public static class MessageStorage
{
public static ObservableCollection<Message> Messages { get; set; }
}
}
并且最后是消息类:
using System;
namespace Messaging
{
public class Message
{
public string MessageContent { get; set; }
public DateTime PublishDate { get; set; }
}
}
我希望有所帮助。