[英]Multiline text in dynamicaly changed TextBlock
Hi so i am trying to push a twitch chat into a TextBlock and it worked fine just using the mvvm with the textblock but now i want to actualy color the username and not sure how to make this multiline because the way i have it now it just replaces the previous message so i would need help to move forward.嗨,所以我正在尝试将 twitch 聊天推送到 TextBlock 中,并且仅使用带有文本块的 mvvm 就可以正常工作,但是现在我想实际为用户名着色,并且不确定如何制作这个多行,因为我现在拥有它的方式只是替换上一条消息,因此我需要帮助才能继续前进。 thanks!\
谢谢!\
Xaml: Xaml:
<TextBlock Grid.Column="1" Grid.Row="1" TextWrapping="Wrap" Background="Gainsboro" FontSize="14" Text="" Margin="5,5,5,5">
<Run Text="{Binding Username, Mode=OneWay}" Foreground="{Binding UsernameColor, Mode=OneWay}" />
<Run Text="{Binding Message, Mode=OneWay}" />
</TextBlock>
Event:事件:
private void Client_OnMessageReceived(object sender, OnMessageReceivedArgs e)
{
Username = $"{e.ChatMessage.DisplayName}:";
Message = e.ChatMessage.Message;
UsernameColor = e.ChatMessage.ColorHex;
}
so the issue is i want it to be multiline and not replace the Runs everytime a message comes thru.所以问题是我希望它是多行的,而不是每次通过消息时都替换运行。
I think you are following the wrong approach.我认为您采用了错误的方法。 You have to see a chat as a collection of messages.
您必须将聊天视为消息的集合。 You want to display each message individually, as they come in. This shouts to use a
ListBox
where each item represents an individual message:您希望在每条消息进入时单独显示它们。这需要使用
ListBox
,其中每个项目代表一条单独的消息:
ChatMessage.cs聊天消息.cs
class ChatMessage : INotifyPropertyChanged
{
private string username;
public string Username
{
get => this.username;
set
{
this.username = value;
OnPropertyChanged();
}
}
private string message;
public string Message
{
get => this.message;
set
{
this.message = value;
OnPropertyChanged();
}
}
private string colorValue;
public string ColorValue
{
get => this.colorValue;
set
{
this.colorValue = value;
OnPropertyChanged();
}
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
ViewModel.cs视图模型.cs
class ViewModel : INotifyPropertyChanged
{
public ObservableCollection<ChatMessage> Messages { get; set; }
private void Client_OnMessageReceived(object sender, OnMessageReceivedArgs e)
{
var chatMessage = new ChatMessage
{
Username = $"{e.ChatMessage.DisplayName}:",
Message = e.ChatMessage.Message
UsernameColor = e.ChatMessage.ColorHex
}
this.Messages.Add(chatMessage);
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
MainWindow.xaml主窗口.xaml
<Window>
<Window.DataContext>
<ViewModel />
<Window.DataContext>
<ListBox ItemsSource="{Binding Messages}"
IsHitTestVisible="False">
<ListBox.ItemTemplate>
<DataTemplate DataType="{x:Type ChatMessage}">
<StackPanel Orientation="Horizonatal">
<TextBlock Text="{Binding Username}"
Foreground="{Binding ColorValue}" />
<TextBlock Text="{Binding Message}" />
</StackPanel>
</DataTemplate >
</ListBox.ItemTemplate>
</ListBox ItemsSource="{Binding Messages}">
</Window>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.