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:
To reference that class, you must include the namespace of that project in the ResourceDictionary element.
Create a converters element below the ResourceDictionary node.
An in your style, you may reference the converter using it's key.
The complete ResourceDictionary code is presented here.
Include the ResourceDictionary in Resource.MergedDictionaries as an Application Resource (App.xaml).
To use the style in your DataGrid control, just set the RowStyle property with the correct key of the DataGridRow style.
Done! :-)
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(); } } }
<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 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>
<Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="/Styles/DataGrid.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources>
<DataGrid ItemsSource="{Binding ListEmployees}" x:Name="EmployeeGrid" RowStyle="{StaticResource dgRow}"
Done! :-)
Comments
Post a Comment