动态更改 TextBlock 中的多行文本
Multiline text in dynamicaly changed TextBlock
嗨,所以我正在尝试将 twitch 聊天推送到 TextBlock 中,仅将 mvvm 与文本块一起使用就可以正常工作,但现在我想实际为用户名着色,但不确定如何制作此多行,因为我的方式现在有了它,它只是取代了以前的消息,所以我需要帮助才能前进。谢谢!\
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>
事件:
private void Client_OnMessageReceived(object sender, OnMessageReceivedArgs e)
{
Username = $"{e.ChatMessage.DisplayName}:";
Message = e.ChatMessage.Message;
UsernameColor = e.ChatMessage.ColorHex;
}
所以问题是我希望它是多行的,而不是每次收到消息时都替换运行。
我认为您采用了错误的方法。您必须将聊天视为消息的集合。您希望在消息进入时单独显示每条消息。这需要使用 ListBox
,其中每个项目代表一条单独的消息:
ChatMessage.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
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
<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>
嗨,所以我正在尝试将 twitch 聊天推送到 TextBlock 中,仅将 mvvm 与文本块一起使用就可以正常工作,但现在我想实际为用户名着色,但不确定如何制作此多行,因为我的方式现在有了它,它只是取代了以前的消息,所以我需要帮助才能前进。谢谢!\
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>
事件:
private void Client_OnMessageReceived(object sender, OnMessageReceivedArgs e)
{
Username = $"{e.ChatMessage.DisplayName}:";
Message = e.ChatMessage.Message;
UsernameColor = e.ChatMessage.ColorHex;
}
所以问题是我希望它是多行的,而不是每次收到消息时都替换运行。
我认为您采用了错误的方法。您必须将聊天视为消息的集合。您希望在消息进入时单独显示每条消息。这需要使用 ListBox
,其中每个项目代表一条单独的消息:
ChatMessage.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
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
<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>