如何做一个进度/消息窗口,其中每行可以是不同的字体,大小,颜色? [英] How to make a Progress/Message Window where each line can be different font, size, color?

查看:214
本文介绍了如何做一个进度/消息窗口,其中每行可以是不同的字体,大小,颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在我的视图中创建一个TextBlock,它将显示我发送给它的消息。它只是在每次它写入时保持附加一个新行。我希望能够以不同的字体,大小和每行的颜色写。

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.

我发现了ListView和RichTextBox的例子。我不在乎什么控制是。它只需要遵循MVVM格式,这是我遇到麻烦的那些例子。

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.

对于那些熟悉命令窗口,你怎么能批处理文件和echo行到显示器?这是我想做的。

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.

推荐答案

找到 ListBox中的交替行颜色,并使用绑定文本块的前景。实现我需要做一个类来保持我的字符串和颜色。将该类放在一个ObservableCollection中,并绑定到ObservableCollection。

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.

我的新类:

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>

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

Where pb is my local class variable in my VM.

模型中的代码:

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 });

这篇关于如何做一个进度/消息窗口,其中每行可以是不同的字体,大小,颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆