SteveCl
SteveCl

Reputation: 4569

Silverlight datagrid, style text in cells

What I am trying to do - I have a grid with some data, lots of it being text, what I want is to be able to hightlight a certain string within all that text....

I have the term...test The text in the cell is "test 123 testing 123"

I want the word test to appear yellow, bold (or any other style I need). I have done this with an asp.net grid by using the datarowbound event, and replacing the string "test" with some html that give the string the required style.

How would I go about doing that in a silverlight cell?

Maybe worth noting, the rows are added to the grid programmatically at runtime...

tcol = new DataGridTextColumn();
tcol.Binding = new System.Windows.Data.Binding("class_property");
tcol.Header = "Header";
tcol.IsReadOnly = true;
dgResults.Columns.Add(tcol);

Thanks in anticipation... Steve

Upvotes: 1

Views: 931

Answers (2)

Johannes
Johannes

Reputation: 1095

From a pure Silverlight perspective, the way to style only parts of a text line (i.e. text in a textblock) is by using the Run element and adding the multiple Run elements to the textblock.

CODE

Run text = new Run();
                Run dates = new Run();
                Run comments = new Run();

                text.Text = y.User;
                dates.Text = " (" + y.TimeStamp.ToShortTimeString() + ")";
                comments.Text = ":"+y.Comment;

                dates.Foreground = new SolidColorBrush(Colors.Blue);

                rpconversation.Inlines.Add(text);
                rpconversation.Inlines.Add(dates);
                rpconversation.Inlines.Add(comments);

will provide text where the user and comments have the standard black text, and the dates will have blue text. You can read more about it on the MSDN site.

However, this forum goes into how to change text elements through javascripting. Maybe have a read through it.

Upvotes: 1

Neil Knight
Neil Knight

Reputation: 48577

There is a LoadingRow event handler which is part of the DataGrid. You can do the same as your ASP .Net in this event.

Upvotes: 0

Related Questions