// To scroll to the bottom when new item is added to the list private void MessagesScrollViewer_ScrollChanged(object sender, ScrollChangedEventArgs e) { if (canScrollToEnd) // If new item is being added to the list. If it false means that user is scrolling manually { MessagesScrollViewer.ScrollToEnd(); canScrollToEnd = false; // The message is no longer being added } }
private void SendMessage_OnClick(object sender, RoutedEventArgs e) { try { Message m = new Message() { DateOfSending = DateTime.Now, From = TeacherOrStudentListView.SelectedItem.GetType() == typeof(Teacher) ? ((Teacher)TeacherOrStudentListView.SelectedItem).TeacherName : ((Student)TeacherOrStudentListView.SelectedItem).StudentName, GroupName = ((Group)GroupListView.SelectedItem).GroupName, Type = TeacherOrStudentListView.SelectedItem.GetType() == typeof(Teacher) ? "Teacher" : "Student", Msg = InputMessageTextBox.Text }; if (UseRabbitMqRadioButton.IsChecked != null && (bool)UseRabbitMqRadioButton.IsChecked) { RestRequest request = new RestRequest("AddMessage/{GroupName}/{Message}", Method.POST); request.AddUrlSegment("GroupName", ((Group)GroupListView.SelectedItem).GroupName); request.AddUrlSegment("Message", InputMessageTextBox.Text); string content; _client.ExecuteAsync(request, response => { content = response.Content; }); PublishMessage(m, m.GroupName); } else { Thread receiveThread = new Thread(Receiver); receiveThread.Start(); Chat.Send(m); //MessageTextBlock.Dispatcher.InvokeAsync(() => //{ // MessageTextBlock.Text += $"\n From : {m.Type} , Name : {m.From} , Date : {m.DateOfSending} \n\tMessage : {m.Msg}"; //}); } MessagesScrollViewer.ScrollToEnd(); InputMessageTextBox.Text = null; } catch (Exception exception) { ErrorOrSuccesTextBlock.Dispatcher.InvokeAsync(() => { ErrorOrSuccesTextBlock.Text += exception; }); } }
private void DisplayReceivedMessage(MessageModel message) { TextBox msg = new TextBox { Text = message.content, Style = (Style)FindResource("ReceivedMessageStyle"), }; Label sender = new Label { Content = message.senderusername + " " + message.timestamp, Style = (Style)FindResource("SenderName"), HorizontalAlignment = HorizontalAlignment.Left }; Messages.Children.Add(sender); Messages.Children.Add(msg); MessagesScrollViewer.ScrollToEnd(); }
public void DisplaySentMessage(Message message) { TextBox msg = new TextBox { Text = message.content, Style = (Style)FindResource("SentMessageStyle"), }; Label sender = new Label { Content = message.senderUsername + " " + message.timestamp, Style = (Style)FindResource("SenderName"), HorizontalAlignment = HorizontalAlignment.Right }; Messages.Children.Add(sender); Messages.Children.Add(msg); MessagesScrollViewer.ScrollToEnd(); }
private void ItemContainerGenerator_ItemsChanged(object sender, System.Windows.Controls.Primitives.ItemsChangedEventArgs e) { MessagesScrollViewer.ScrollToEnd(); }