Hi @EdgeMac ,
Welcome to Microsoft Q&A!
Using VisualTreeHelper, we can find that the text elements in DataGrid
are stored in TextBlock
. We can use VisualTreeHelper
to read all TextBlocks
in DataGrid
and highlight the text we want. This example is just a simple demonstration and can only find the first matching text. You need to continue to improve it.
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" VerticalAlignment="Center">
<TextBox x:Name="searchTextBox" Text="Banner"></TextBox>
<Button x:Name="myButton" Click="myButton_Click">Click Me</Button>
<controls:DataGrid x:Name="EmployeeGrid"
ItemsSource="{x:Bind Persons}"
AutoGenerateColumns="False">
<controls:DataGrid.Columns>
<controls:DataGridTextColumn Header="First Name"
Binding="{Binding FirstName}"/>
<controls:DataGridTextColumn Header="Last Name"
Binding="{Binding LastName}"/>
<controls:DataGridTextColumn Header="Position"
Binding="{Binding Position}"/>
<!--<controls:DataGridTemplateColumn Header="First Name">
<controls:DataGridTemplateColumn.CellEditingTemplate>
<DataTemplate>
<TextBlock Text="{Binding FirstName}"/>
</DataTemplate>
</controls:DataGridTemplateColumn.CellEditingTemplate>
</controls:DataGridTemplateColumn>-->
</controls:DataGrid.Columns>
</controls:DataGrid>
public sealed partial class MainWindow : Window
{
public List<Department> Departments { get; set; }
public List<Person> Persons { get; set; }
public MainWindow()
{
this.InitializeComponent();
Departments = new List<Department>
{
new Department {DepartmentId = 1, DepartmentName = "R&D"},
new Department {DepartmentId = 2, DepartmentName = "Finance"},
new Department {DepartmentId = 3, DepartmentName = "IT"}
};
Persons = new List<Person>
{
new Person
{
PersonId = 1, DepartmentId = 3, FirstName = "Ronald", LastName = "Rumple",
Position = "Network Administrator"
},
new Person
{
PersonId = 2, DepartmentId = 1, FirstName = "Brett", LastName = "Banner",
Position = "Software Developer"
},
new Person
{
PersonId = 3, DepartmentId = 2, FirstName = "Alice", LastName = "Anderson",
Position = "Accountant"
}
};
}
private void myButton_Click(object sender, RoutedEventArgs e)
{
myButton.Content = "Clicked";
var searchString= searchTextBox.Text.Trim();
List<TextBlock> txtBlocklist = new List<TextBlock>();
FindChildren(txtBlocklist, EmployeeGrid);
foreach (TextBlock txtBlock in txtBlocklist)
{
txtBlock.TextHighlighters.Clear();
int index = txtBlock.Text.IndexOf(searchString);
if (index >= 0 )
{
TextRange textRange = new TextRange() { StartIndex = index, Length = searchString.Length };
TextHighlighter highlighter = new TextHighlighter()
{
Background = new SolidColorBrush(Colors.Yellow),
Ranges = { textRange }
};
//add the highlighter
txtBlock.TextHighlighters.Add(highlighter);
}
}
}
internal static void FindChildren<T>(List<T> results, DependencyObject startNode) where T : DependencyObject
{
int count = VisualTreeHelper.GetChildrenCount(startNode);
for (int i = 0; i < count; i++)
{
DependencyObject current = VisualTreeHelper.GetChild(startNode, i);
if ((current.GetType()).Equals(typeof(T)) || (current.GetType().GetTypeInfo().IsSubclassOf(typeof(T))))
{
T asType = (T)current;
results.Add(asType);
}
FindChildren<T>(results, current);
}
}
}
//backing data source in MyViewModel
public class Department
{
public int DepartmentId { get; set; }
public string DepartmentName { get; set; }
}
public class Person
{
public int PersonId { get; set; }
public int DepartmentId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Position { get; set; }
}
Thank you.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.