Very often, while developing WPF applications, we actually feel the need for reusable/redistributable controls. WPF custom controls are shipped in DLL format such that they can be consumed in various applications. Please note, there is a significant difference between custom control and user control. They are different things.
Custom controls basically have one code file and one resource file which has the all the definitions of the layout and binding information.
To explain the functionality, today we will make a very simple WPF custom DataGrid control in which there will be an inbuilt FullTextSearch box and also you can hide-unhide based on your needs.
Project structure

I have created a very basic solution which contains,
- 1 WPF custom control library (SimpuControls) – Which will be shipped as SimpuControls.DLL
- 1 WPF application project (WpfApp1) – To test our custom control.
Let’s develop the SimpuDataGrid which is the custom DataGrid we talked about above. First right click on SimpuControls project and add CustomControl(WPF) to it. Once you add, it will automatically create a cs file and an XAML file. The XAML file is named as Generic.xaml inside Themes folder. Per our requirement, we will be needing two Converters – we will keep them inside a separate “Converter” folder.

Note
Converters are used when you need to interpret one or more type of values to some different type required for the UI element.
BooleanToVisibilityConverter
- namespace SimpuControls.Converters
- {
- public class BooleanToVisibilityConverter : IValueConverter
- {
- // If the value is 'true' it will be interpreated as 'Visible' else 'Collapsed'
- public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
- {
- if ((bool)value)
- return Visibility.Visible;
- return Visibility.Collapsed;
- }
- public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
- {
- throw new NotImplementedException();
- }
- }
- }
SearchTextToVisibilityConverter
This is a Multi-Value converter which receives an array of objects as input. The first element of the array is the “Search Text” and the second element is the entire object bound to the current grid row. Once these two objects are received, the SearchText is searched in all the properties of the second object using reflection. If found anywhere, return Visible else Collapsed.
- namespace SimpuControls.Converters
- {
- public class SearchTextToVisibilityConverter : IMultiValueConverter
- {
- public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
- {
- if ((string)values[0] == "") return Visibility.Visible;
- var searchText = ((string)values[0]).ToLower();
- var row = values[1];
- foreach (var p in row.GetType().GetProperties())
- {
- if (System.Convert.ToString(p.GetValue(row)).ToLower().Contains(searchText))
- return Visibility.Visible;
- }
- return Visibility.Collapsed;
- }
- public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
- {
- throw new NotImplementedException();
- }
- }
- }
SimpuControl.cs
The below code shows the custom control i.e SimpuControl class inherits from DataGrid base class. And here we have purposefully added EnableFullTextSearch Boolean dependency property to make user capable of hide/unhide FullTextSearch textbox attached at the top of the DataGrid.
Note
Dependency properties are called Dependency properties because, we can inject dependency from outside through this property.
- namespace SimpuControls
- {
- public class SimpuDataGrid : DataGrid
- {
- static SimpuDataGrid()
- {
- DefaultStyleKeyProperty.OverrideMetadata(typeof(SimpuDataGrid),
- new FrameworkPropertyMetadata(typeof(SimpuDataGrid)));
- }
- // This property gives you flexibility to hide or unhide FullTextSearch functionality
- public static readonly DependencyProperty EnableFullTextSearchProperty =
- DependencyProperty.Register("EnableFullTextSearch", typeof(bool),
- typeof(SimpuDataGrid), new UIPropertyMetadata(false));
- public bool EnableFullTextSearch
- {
- get
- {
- return (bool)GetValue(EnableFullTextSearchProperty);
- }
- set
- {
- SetValue(EnableFullTextSearchProperty, value);
- }
- }
- }
- }



Mario SillmannPosted Jul 16, 2020, 7:54 AM
The whole thing breaks if i set AutoGenerateColumns to false. Any ideas?
mayle adenPosted Sep 26, 2019, 8:20 PM
I got an issue of BooleanToVisibilityConverter does not exist in namespace....
mayle adenPosted Sep 26, 2019, 8:17 PM
Thanks Swaraj Ketan
Jeremy GrahamPosted Mar 26, 2019, 7:35 PM
Thanks mate, very useful. Is there a way to change the Simpu DataGrid to AutoGenerate - False to remove the empty column?
Shubham PathakPosted Jul 30, 2018, 2:32 AM
This is very useful code. Can you please tell me how to highlight the search results? I tried but not getting the desired output.
Monika SharmaPosted Jun 28, 2018, 1:52 AM
Awesome explanation!! Thanks for sharing.