如何创建一个进度/消息窗口,其中每一行可以是不同的字体,大小,颜色?

时间:2022-06-08 14:56:05

I tried to make a TextBlock in my View that will display whatever message I send to it. It just keeps appending a new line each time it writes. I want the ability to write in different font, size, and color per line.

我试图在我的视图中创建一个TextBlock,它将显示我发送给它的任何消息。它每次写入时都会不断添加新行。我希望能够以每行不同的字体,大小和颜色进行书写。

I have found examples that do it for ListViews, and RichTextBox. I don't care what control it is. It just needs to follow the MVVM format and that is where I am having troubles with those examples.

我找到了为ListViews和RichTextBox执行此操作的示例。我不在乎它是什么控制。它只需要遵循MVVM格式,这就是我遇到这些例子的麻烦。

For those that are familiar with the Command Window, how you can make a batch file and 'echo' lines to the display? That is what I am trying to do.

对于熟悉命令窗口的人,如何制作批处理文件并将“回显”行显示在显示屏上?这就是我想要做的。

1 个解决方案

#1


1  

Found Alternate row color in Listbox and used Bind foreground of Textblock. Realized I needed to make a class to hold my string and color. Put that class in an ObservableCollection, and bind to the ObservableCollection.

在列表框中找到备用行颜色,并使用文本块的绑定前景。意识到我需要创建一个类来保存我的字符串和颜色。将该类放在ObservableCollection中,并绑定到ObservableCollection。

My new class:

我的新班:

public class DisplayData
{
    public string _string { get; set; }
    public System.Windows.Media.Brush _color { get; set; }
    public int _fontSize { get; set; }
}

XAML:

<ListBox x:Name="Progress_Window" ItemsSource="{Binding pb._displayString}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding _string}" Foreground="{Binding _color}" FontSize="{Binding _fontSize}" />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Where pb is my local class variable in my VM.

其中pb是我的VM中的本地类变量。

Code in Model:

型号代码:

public ObservableCollection<DisplayData> _displayString { get; set; }
...
_displayString = new ObservableCollection<DisplayData>();
string _error = "Error Opening COM Port";
_displayString.Add(new DisplayData { _string = _error, _color = System.Windows.Media.Brushes.Red, _fontSize = 20 });

#1


1  

Found Alternate row color in Listbox and used Bind foreground of Textblock. Realized I needed to make a class to hold my string and color. Put that class in an ObservableCollection, and bind to the ObservableCollection.

在列表框中找到备用行颜色,并使用文本块的绑定前景。意识到我需要创建一个类来保存我的字符串和颜色。将该类放在ObservableCollection中,并绑定到ObservableCollection。

My new class:

我的新班:

public class DisplayData
{
    public string _string { get; set; }
    public System.Windows.Media.Brush _color { get; set; }
    public int _fontSize { get; set; }
}

XAML:

<ListBox x:Name="Progress_Window" ItemsSource="{Binding pb._displayString}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding _string}" Foreground="{Binding _color}" FontSize="{Binding _fontSize}" />
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

Where pb is my local class variable in my VM.

其中pb是我的VM中的本地类变量。

Code in Model:

型号代码:

public ObservableCollection<DisplayData> _displayString { get; set; }
...
_displayString = new ObservableCollection<DisplayData>();
string _error = "Error Opening COM Port";
_displayString.Add(new DisplayData { _string = _error, _color = System.Windows.Media.Brushes.Red, _fontSize = 20 });