1

I'm creating a little app that will read, parse, and format the contents of a lua script - specifically, a SavedVariables file from World of Warcraft created by the "Elephant" chat logging add-on. The prototype I have in mind will generate an array of ListViewItem instances, showing a timestamp, player name, what channel the message was posted into, and the message that was posted.

I'd like to have the chat message rendered as it would be in the game: the game colors the names of those "talking" in the chat after their character's class (e.g. a rogue's name is yellow, mages' names are light blue), but when it comes to ListViewItem and ListViewSubItem, coloring text there seems to be an all-or-nothing deal.

Would it be possible to add functionality for the ListView to apply formatting (or just color) to text in a ListViewItem or ListViewSubItem based on markup in the Text property?

EDIT: I'm asking if it's possible to just add text formatting to a ListViewItem through the magic of inheritance/polymorphism in a derived class, and if so, what's the best way to do it (amount of effort, code security, etc)?

p0lar_bear
  • 2,203
  • 2
  • 21
  • 31
  • It would seem that you'll have to create a custom `ListView` control. See [here](http://stackoverflow.com/a/11323/1244630) for a similar question about formatting a `Label`. – Pooven Jul 25 '12 at 16:33
  • I figured that I'd have to make a custom ListView, ListViewItem, or ListViewSubItem. I wasn't clear enough - I'm asking if it's possible to just add text formatting to a ListViewItem through the magic of inheritance/polymorphism in a derived class, and what's the best way to do it (amount of effort, code security, etc). I'll edit my question to reflect this. – p0lar_bear Jul 26 '12 at 13:12

2 Answers2

1

Speaking from a Windows Forms usage view point, you can render the items manually. However you should also consider situations such as: how should the text be rendered when the text exceeds the bounds of the Control/column?

This only requires that you inherit from ListView; you can render each item using:

protected override void OnDrawItem(DrawListViewItemEventArgs e)

Of course, OwnerDraw must be set to true. DrawListViewItemEventArgs contains the text that needs to be rendered and the Graphics instance that should be used to render the text. You could then use DrawString(string s, Font font, Brush brush, float x, float y) to render each part of the text as required by your mark-up convention.

If you don't mind the GPL, ObjectListView does offer more formatting options but perhaps it is not as flexible as you require.

Since a RichTextBox does allows flexible text formatting, I wonder if it could somehow be used as a renderer...

Pooven
  • 1,744
  • 1
  • 25
  • 44
0

im not completely sure this would work and I cant really test it, but it might be worth a try

<ListViewItem>
   <ContentPresenter>
       <TextBlock>
          <Run x:Name="playerName" Foreground="Blue" Text="NAME"/>
          <Run x:Name="etc" Foreground="Black" Text="..."/>
        </TextBlock>
     </ContentPresenter>
 </ListViewItem>

The basic idea here is that in the list box item you have a the content presenter that I think holds the textblock. Within this you can set individual Run tags (bits of individual text) and you can format each one differently. you should also be able to set the text programmatically when the run tags are assigned names

J.B
  • 1,642
  • 2
  • 15
  • 33