IValueConverter in WPF Data Binding Example : Binding datagridview in wpf using IValueConverter to convert the value during data binding.
Incoming search terms
IValueConverter in WPF Data Binding Example, Value conversion with IValueConverter – The complete WPF tutorial, WPF Introduction: Databinding + IValueConverter, IValueConverter in WPF data binding, WPF Tutorial | Value Converters, wpf – Databinding on a IValueConverter, WPF Data Binding and IValueConverter.
Introduction
If you want to databind two properties that have incompatible types, you need a piece of code in between, that converts the value from source to target type and back. This piece of code is called ValueConverter. A value converter is a class, that implements the simple interface IValueConverter with the two methods object Convert(object value) and object ConvertBack(object value).
See below image for more understanding…
When to use a value converter
Value converters are very frequently used with data bindings. Here are some basic examples:
- You want to check a CheckBox based on a value, but the value is a string like “yes” or “no” instead of a Boolean value
- You have a file size in bytes but you wish to show it as bytes, kilobytes, megabytes or gigabytes based on how big it is
- You have a result values but you want to show that values in different colors depends on the value
Ok..!! Let’s implement IValueConverter in one of the real example.
Here we are planning to display the records in datagird with result status like Pass/Fail. So for that we use the IValueConverter in WPF. Let’s move on the example…
Following are the steps to create example of IValueConverter.
ADD XAML CODE
<Window x:Class="MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:IValue_Converter" Title="Code Scratcher" Height="350" Width="525"> <Window.Resources> <local:GetStatus x:Key="GetStatusKey" /> </Window.Resources> <Grid> <DataGrid Name="DGRecords" IsReadOnly="False" AutoGenerateColumns="False" Cursor="Arrow" CanUserAddRows="False" AlternationCount="2" Margin="0,0,0,0"> <DataGrid.Columns> <DataGridTextColumn Binding="{Binding Id}" Header="Id" MinWidth="100" /> <DataGridTextColumn Binding="{Binding Name}" Header="Name" MinWidth="100" /> <DataGridTextColumn Binding="{Binding Result}" Header="Result (%)" MinWidth="100" /> <DataGridTextColumn Binding="{Binding Result, Converter={StaticResource GetStatusKey}}" Header="Status (Pass/Fail)" MinWidth="100" /> </DataGrid.Columns> </DataGrid> </Grid> </Window>
See below image for more understanding….
ADD CODE BEHIND CODE
Imports System.Data Class MainWindow Dim dtTemp As DataTable = New DataTable("DT") Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded dtTemp.Columns.Add("Id") dtTemp.Columns.Add("Name") dtTemp.Columns.Add("Result") dtTemp.Rows.Add("1", "Name1", "85") dtTemp.Rows.Add("2", "Name2", "32") dtTemp.Rows.Add("3", "Name3", "40") dtTemp.Rows.Add("4", "Name4", "30") DGRecords.ItemsSource = dtTemp.AsDataView() End Sub End Class Public Class GetStatus Implements IValueConverter Public Function Convert(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert If value > 35 Then Return "Pass" Else Return "Fail" End If End Function Public Function ConvertBack(ByVal value As Object, ByVal targetType As System.Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack Return New Object() End Function End Class
Don’t forget to add namespace in code behind code.
RUN PROJECT AND CHECK FINAL OUTPUT
Source Code
help@codescratcher.com
The post IValueConverter in WPF Data Binding Example appeared first on Code Scratcher.