我正在开发一个允许用户在一个文本框中输入注释的WPF项目,当他/她按下按钮时,所写的注释会被发布到另一个文本框中,除此之外,最后发布的评论需要发布在上一个。因此,如果例如评论1在晚上10点发布而另一个在晚上11点发布,那么晚上11点的评论将放在晚上10点的评论之上。我怎么能这样做?
<TextBox x:Name="txtWriteComments" MinLines="4"/>
<Button x:Name="btnPostComments" Content="Post Comment" Click="btnPostComments_Click"/>
主窗口中的代码
private void btnPostComments_Click(object sender, RoutedEventArgs e)
{
int i = 0;
TimeSpan time = DateTime.Now.TimeOfDay;
do
{
i++;
???
} while (i < 6);
}
我做了计数器,因为它只允许最多5条评论
答案 0 :(得分:1)
我会使用ItemsControl(可能是ListBox)来显示所有注释。你可以使用TextBox和Button将它放在你的XAML中。控件的ItemsSource应该类似于ObservableCollection。我会把它留给你,但我们称之为“评论”。您可以在顶部添加新注释,并从底部继续删除注释(5)。
private void btnPostComments_Click(object sender, RoutedEventArgs e)
{
string comment = string.Format("{0}: {1}", DateTime.Now.TimeOfDay, txtWriteComments.Text);
Comments.Insert(0, comment);
if(Comments.Count > 5)
Comments.RemoveAt(5);
}
此外,您可能希望在将TextBox添加到ListBox后清除它的值。你也可以在这里做到这一点。