-2

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.

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.

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.

Mike Lischke
  • 48,925
  • 16
  • 119
  • 181
Bluto
  • 166
  • 1
  • 15
  • 2
    @afuzzyllama - You can edit, but you can't help? – Bluto Jan 03 '13 at 20:42
  • ListViews and RichTextBox would be the ideal controls. Perhaps provide more detail as to what you've tried and/or how the examples are confusing you with respect to MVVM pattern. – Backlash Jan 03 '13 at 21:19
  • 1
    @MarkLallemont - I was reviewing your question for the community. It does not mean I know the answer. – afuzzyllama Jan 04 '13 at 01:05
  • Can you share what examples you have looked at that you are having trouble with? How are you calling this view? Asynchronously? – afuzzyllama Jan 04 '13 at 01:07

1 Answers1

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.

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.

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 });
Community
  • 1
  • 1
Bluto
  • 166
  • 1
  • 15