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
any help guys. Very urgent
this code is coloring the text but strange behaviour like sometimes colors sometimes not.
Thanks in advance