Posts

Showing posts with the label WPF Value Converter

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"

WPF DataGrid Excel Comment Indicator

Image
Here's how to add a comment indicator to a DataGridCell similar to an Excel functionality using MultiValueConverter. See codefiles and screenshot below. XAML 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 <Window x:Class= "WPFExcelCommentIndicator.DataGridCellCommentIndicator" xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x= "http://schemas.microsoft.com/winfx/2006/xaml" xmlns:src= "clr-namespace:WPFExcelCommentIndicator" Title= "DataGridCellCommentIndicator" Height= "350" Width= "500" WindowStartupLocation= "CenterScreen" > <Window.Resources> <src:DataGridCellComment x:Key= "DataGridCellComment" /> <src:StudentList x:Key= "StudentListData" /> </W

ListView In WPF With Alternating Row Colors

Here's how to alternate the row colors for a WPF ListView control using a Converter class and Triggers. Converter Class: 1 2 3 4 5 6 7 8 9 10 11 12 13 public class AddressTargetConverter : IValueConverter { public object Convert( object value , Type targetType, object parameter, CultureInfo culture) { return ( value .ToString().Equals(parameter.ToString())); } public object ConvertBack( object value , Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); } } XAML: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 <Window.Resources> <y:AddressTargetConverter x:Key= "AddressTargetConverter" /> </Window.Resources> <Grid> <StackPanel> <ListView ItemsSource= "{Binding PersonCollection}" > <ListView.It

Change WPF DataGridCell Background Color Using IVMultiValueConverter

Image
In a post by codefornothing in which He demonstrate the use of IMultiValueConverter using DataTable: The WPF Datagrid and Me , I decided to create a similar post in which the binding source is of type collection (Observable Collection). I encountered a few issues like how to replace the Binding Path values from binding to a strongly type class instead of DataTable. To get things started, here are the snippets: Observable Class public class StudentList : ObservableCollection<Student> { public StudentList() { Add( new Student{ID = 1, Age = 29, Name = "Jerby" , Address= "Cebu" }); Add( new Student{ID = 2, Age = 28, Name = "Renz" , Address= "Cebu" }); Add( new Student{ID = 3, Age = 23, Name = "Aya" , Address= "Leyte" }); Add( new Student{ID = 4, Age = 33, Name = "Jeff" , Address= "Leyte" }); Add( new Student{ID = 5

Change WPF DataGridCell Background Color Using IValueConverter

Image
Here's a solution in painting a specific DataGridCell using IValueConverter. The datagrid set's it's ItemSource property either using List object or Datatable object. Resource markup: <Window.Resources> <src:BorderBrushConverter x:Key= "BorderBrushConverter" /> </Window.Resources> XAML markup: <DataGrid AutoGenerateColumns= "False" CanUserAddRows= "False" Grid.Row= "2" Grid.Column= "0" Name= "dgStudents1" > <DataGrid.Columns> <DataGridTextColumn Header= "ID" Binding= "{Binding Path=ID}" Width= "120" IsReadOnly= "True" /> <DataGridTextColumn Header= "Age" Binding= "{Binding Path=Age}" MinWidth= "100" IsReadOnly= "True" > <DataGridTextColumn.CellStyle> <Style TargetType= "DataGridCell"

Change WPF DataGridRow Background Color Using IValueConverter

Image
There are several ways in painting a wpf datagrid. One option would be to use the IValueConverter interface. Firstly, you have to define a class that implements the interface. And add contracts to Convert() and ConvertBack() methods. Assuming in your form load, you bind a List object to the datagrid's ItemSource property. T could be a pre-defined class. Here's the Resource code: <Window.Resources> <src:AgeTargetConverter x:Key= "AgeTargetConverter" /> </Window.Resources> Here's the XAML markup: <DataGrid Grid.Row= "0" Grid.Column= "0" AutoGenerateColumns= "False" CanUserAddRows= "False" Name= "dgStudents" > <DataGrid.RowStyle> <Style TargetType= "{x:Type DataGridRow}" > <Style.Triggers> <DataTrigger Binding= "{Binding Age, Converter={StaticResource AgeTargetConverter}, ConverterP

Donate