I need a RichTextbox Colored in celltemplate inside RadGridView

1 Answer 35 Views
GridView RichTextBox
Deepak
Top achievements
Rank 1
Deepak asked on 07 Oct 2021, 01:40 PM | edited on 07 Oct 2021, 01:55 PM

I have a RadGridView inside that 2 different column i have a cell template and a datatemplate in that i have the RichTextbox inside that paragraph inside that run and the text property binded on it.

 

Now i need to compare tow richtexbox text value. If any RichTextbox value is different it need to be highlighted inside thta text Box.

XAML

                    

<RadGridView Grid.Row="1" x:Name="gfgvBrokenLinks" AutoGenerateColumns="False" ItemsSource="{Binding BrokenLinks}" IsReadOnly="True" AutoExpandGroups="False" RowLoaded="gfgvBrokenLinks_RowLoaded">

<telerik:GridViewDataColumn DataMemberBinding="{Binding Data1}"  Header="Data1" IsGroupable="False">
                        <telerik:GridViewDataColumn.CellTemplate>
                            <DataTemplate>
                                <RichTextBox x:Name="richTextBox2" MinWidth="100" IsReadOnly="True">
                                    <FlowDocument >
                                        <Paragraph>
                                            <Run Text="{Binding Data1,Mode=OneWay}"></Run>
                                        </Paragraph>
                                    </FlowDocument>
                                </RichTextBox>
                            </DataTemplate>
                        </telerik:GridViewDataColumn.CellTemplate>
                    </telerik:GridViewDataColumn>

                    <telerik:GridViewDataColumn DataMemberBinding="{Binding Data2}" Header="{my:LocString ResourceKey=HeaderData2}" IsGroupable="False" >
                        <telerik:GridViewDataColumn.CellTemplate>
                            <DataTemplate>
                                <RichTextBox x:Name="richTextBox3" MinWidth="100" IsReadOnly="True" >
                                    <FlowDocument >
                                        <Paragraph>
                                            <Run Text="{Binding Data2,Mode=OneWay}"></Run>
                                        </Paragraph>
                                    </FlowDocument>
                                </RichTextBox>
                            </DataTemplate>
                        </telerik:GridViewDataColumn.CellTemplate>
                    </telerik:GridViewDataColumn>

 

 

 

c#


private void gfgvBrokenLinks_RowLoaded"object sender, Telerik.Windows.Controls.GridView.RowLoadedEventArgs e)
{



if (e.Row.Cells.Count > 0)
{
RichTextBox rt1 = e.Row?.Cells[8]?.Content as RichTextBox;
RichTextBox rt2 = e.Row?.Cells[9]?.Content as RichTextBox;
if (rt1 != null && rt2 != null)
{
string a1 = StringFromRichTextBox(rt1);
string a2 = StringFromRichTextBox(rt2);
if (!a1.Equals(a2))
{
int startindex = 0;
startindex = a1.Zip(a2, (c1, c2) => c1 == c2).TakeWhile(b => b).Count();
int length = a2.Length - startindex;
Select(rt2, startindex, length, Colors.Yellow);
}



//string text = new TextRange(rt2.Document.ContentStart, rt2.Document.ContentEnd).Text;
//if (text.Length > 0)
//{
// TextPointer myTextPointer1 = rt2.Document.ContentStart.GetPositionAtOffset(startindex + 1);
// TextPointer myTextPointer2 = rt2.Document.ContentEnd;
// if (myTextPointer1 != null && myTextPointer2 != null)
// {
// TextRange tr = new TextRange(myTextPointer1, myTextPointer2);
// tr.ApplyPropertyValue(TextElement.BackgroundProperty, Brushes.Yellow);
// }
//}
}
}
}



private static TextPointer GetTextPointAt(TextPointer from, int pos)
{
TextPointer ret = from;
int i = 0;



while ((i < pos) && (ret != null))
{
if ((ret.GetPointerContext(LogicalDirection.Backward) == TextPointerContext.Text) || (ret.GetPointerContext(LogicalDirection.Backward) == TextPointerContext.None))
{
i++;
}



if (ret.GetPositionAtOffset(1, LogicalDirection.Forward) == null)
{
return ret;
}



ret = ret.GetPositionAtOffset(1, LogicalDirection.Forward);
}



return ret;
}



internal string Select(RichTextBox rtb, int offset, int length, Color color)
{
// Get text selection:
TextSelection textRange = rtb.Selection;



// Get text starting point:
TextPointer start = rtb.Document.ContentStart;



// Get begin and end requested:
TextPointer startPos = GetTextPointAt(start, offset);
TextPointer endPos = GetTextPointAt(start, offset + length);



// New selection of text:
textRange.Select(startPos, endPos);



// Apply property to the selection:
textRange.ApplyPropertyValue(TextElement.BackgroundProperty, new SolidColorBrush(color));



// Return selection text:
return rtb.Selection.Text;
}

private string StringFromRichTextBox(RichTextBox rtb)
{
TextRange textRange = new TextRange(rtb.Document.ContentStart, rtb.Document.ContentEnd);
return textRange.Text.Replace("\r\n", "");
}

Thanks In Advance

Deepak
Top achievements
Rank 1
commented on 08 Oct 2021, 05:10 AM

any help guys. Very urgent

this code is coloring the text but strange behaviour like sometimes colors sometimes not.

 

Thanks in advance

1 Answer, 1 is accepted

Sort by
0
Dilyan Traykov
Telerik team
answered on 12 Oct 2021, 09:15 AM

Hi Deepak,

Thank you for the provided code snippets.

I want to start by stating that it is not recommended to work directly with the visual elements (such as GridViewCells in this case and their Content) due to the control's UI virtualization mechanism. This is most probably the reason why the approach you've taken works only partially. You can try to disable the row/column virtualization if you're not working with large amounts of data, but in general, this is not advisable.

Instead, what I can suggest is to determine the differences between the data in the models and expose separate properties which you can then bind in the custom CellTemplate you define.

I hope you find this information helpful.

Regards,
Dilyan Traykov
Progress Telerik

Remote troubleshooting is now easier with Telerik Fiddler Jam. Get the full context to end-users' issues in just three steps! Start your trial here - https://www.telerik.com/fiddler-jam.
Tags
GridView RichTextBox
Asked by
Deepak
Top achievements
Rank 1
Answers by
Dilyan Traykov
Telerik team
Share this question
or