Donate

How To Reference A Converter Class In A Resource Dictionary In WPF

Good evening all!
Given that you have a Resource Dictionary file that has a Style property that will use a Converter class for Binding, the steps to reference that class are as follows. Assuming that you have a converter class such as an EmployeeTargetConverter:
namespace EmployeeManagement
{
 public class EmployeeTargetConverter : IValueConverter
 {
  public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  {
   //converter codes here.....
  }

  public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
  {
   throw new NotImplementedException();
  }
 }
}
 
To reference that class, you must include the namespace of that project in the ResourceDictionary element.
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:EmployeeManagement.Styles"
                    xmlns:converters="clr-namespace:EmployeeManagement">
Create a converters element below the ResourceDictionary node.
<converters:EmployeeTargetConverter x:Key="EmployeeTargetConverter"/>
An in your style, you may reference the converter using it's key.
<Style x:Key="dgRow" TargetType="{x:Type DataGridRow}">
   <Style.Triggers>
    <DataTrigger Binding="{Binding Record, Converter={StaticResource EmployeeTargetConverter}, ConverterParameter = null}" Value="True">
     <Setter Property="Background" Value="#D6EAF8"/>
    </DataTrigger>
   </Style.Triggers>
  </Style>
The complete ResourceDictionary code is presented here.
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:local="clr-namespace:EmployeeManagement.Styles"
                    xmlns:converters="clr-namespace:EmployeeManagement">
     
    <converters:EmployeeTargetConverter x:Key="EmployeeTargetConverter"/>
 <Style x:Key="dgRow" TargetType="{x:Type DataGridRow}">
  <Style.Triggers>
   <DataTrigger Binding="{Binding Record, Converter={StaticResource EmployeeTargetConverter}, ConverterParameter = null}" Value="True">
    <Setter Property="Background" Value="#D6EAF8"/>
   </DataTrigger>
  </Style.Triggers>
 </Style>

</ResourceDictionary>
Include the ResourceDictionary in Resource.MergedDictionaries as an Application Resource (App.xaml).
<Application.Resources>
 <ResourceDictionary>
  <ResourceDictionary.MergedDictionaries>
   <ResourceDictionary Source="/Styles/DataGrid.xaml" />
  </ResourceDictionary.MergedDictionaries>
 </ResourceDictionary>
</Application.Resources>
To use the style in your DataGrid control, just set the RowStyle property with the correct key of the DataGridRow style.
<DataGrid ItemsSource="{Binding ListEmployees}" x:Name="EmployeeGrid" RowStyle="{StaticResource dgRow}"


Done! :-)

Comments

Donate

Popular Posts From This Blog

WPF CRUD Application Using DataGrid, MVVM Pattern, Entity Framework, And C#.NET

TypeScript Error Or Bug: The term 'tsc' is not recognized as the name of a cmdlet, function, script file, or operable program.

Invalid nested tag div found, expected closing tag input