views:

263

answers:

2

Hi, 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

A: 

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

Ardman
Yeah I use that event for some other things...but can I style the content of the cell with html tags? I didn't think i could in silverlight?
SteveCl
I already have an application that uses this technique, so I'll adapt it to try and "style" the text.
Ardman
I've tried to adjust the application and I can't get the Value to specify HTML and render as HTML. It just displays the Value with the HTML. I don't think this can be done. Unless someone else in the community can help?
Ardman
Yes I found the same, I am thinking of someway to maybe have a custom column and dynamically add different styled textblocks maybe?
SteveCl
+1  A: 

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.

Johannes
Excellent. Changing in js is not really something I want to get into. Sounds a little hacky...I would rather write a DataGridColumn or if possible change how the DataGridTextColumn renders to add in some Runs?? is possible or not?? Thx
SteveCl
You can add Runs in XAML (as shown in the MSDN link). However, it has to have a constant layout. So if the text is going to vary then you will have to add the Runs in code-behind.
Johannes
code-behind is the only way I can do it...has to be done at runtime. I'll read the article properly later - only had chance for a quick whizz at the mo...thx.
SteveCl