A WPF solution to display chat messages of an IRC application

820 views Asked by At

I am creating my first WPF application, which is an IRC chat program. Since I am new at WPF, I don't really know how should I display the messages, because they can contain bold, italic words or even images or a quiting message has other color then a simple message.

I was searching on the net and I found some solutions. First type of the solutions is using a RichTextBox with a FlowDocument or just a simple FlowDocument and hack their binding system somehow to display the value of a property. About "hacking" I mean, that these controls doesn't have binding properties, because they are not DependencyObjects. (I am not sure that this is the right expression).

In just a simple TextBlock I can't display the messages, because it displays the value of a string. After having a lot of messages in a simple string and adding a new one to it needs a lot of memory copy, since a string can't be modified. And furthermore if I use a TextBlock, then can I select it's content with my mouse to copy from it? If I use a readonly TextBox, then can I use styling for only one word for example?

So I am about to use a ListView or something that can display the content of a List (or any collection) and then modify its template to display each message correctly. (And disable selection of the ListView somehow) But I don't know what control should I use for the messages to display them stylish and to make them selectable.

Using a list to store the messages would be cool, because after having ~1000 messages, then I would like to remove the first 100 ones to save them in a log file.

So I am totally confused about how to solve this problem. I hope somebody will have a proper solution for this kind of problem!

1

There are 1 answers

5
Erti-Chris Eelmaa On

Well, as you know there are a lot of possibilities.

One of the possibilities is to have ObservableCollection<string> where each element is HTML. You can use ListBox/ItemsControl to bind these lines. On ItemsControl DataTemplate you can add TextBlock element and bind it's Text property against the string.

The trick is to use Converter that can modify Inlines collection of TextBlock and generate concrete Run() elements for TextBlock from HTML string. Note that TextBlock can underlyne/each word different color and many more useful things. Internet is full of such projects(google TextBlock display HTML).

It's going to be trickier to implement Image. As far I know, you can't put it into TextBlock. It depends on your requirements how the image is allowed to insert. You might need to split ONE line into "multiple" TextBlocks, where there is Image() element between.

And then there is RichTextDocument, however it doesn't sound too good for an IRC client. YOu don't need a lot of that functionality, it will complicate things and it's slower.