Posts

Showing posts with the label WPF DataGrid

Donate

WPF DataGrid Data Binding Using MVVM Pattern

Image
Hello, I've been developing WPF applications before but not having applied the MVVM design pattern and have been wanting to create a simple program that loads data into the DataGrid control. As a result of free time, here's a simple demonstration on using MVVM Pattern for Data Binding a WPF DataGrid control. According to Wikipedia, the components of an MVVM pattern are: Model Model refers either to a domain model, which represents real state content (an object-oriented approach), or to the data access layer, which represents content (a data-centric approach). View As in the MVC and MVP patterns, the view is the structure, layout, and appearance of what a user sees on the screen. View model The view model is an abstraction of the view exposing public properties and commands. Instead of the controller of the MVC pattern, or the presenter of the MVP pattern, MVVM has a binder. In the view model, the binder mediates communication between the view and the data binder.The view

WPF DataGrid Set RowBackground Using AlternationCount And DataTriggers

Hi, There was a question raised on how to set the WPF DataGrid RowBackground with AlternationCount (alternating row colors) and DataTriggers without overriding the Alternation count. The fix is to declare a DataGrid.Style that contains settings for RowBackGround and AlternationCount. And in your DataGrid.RowStyle, define the DataTriggers to highlight RowBackground based on a given Value. <DataGrid x:Name= "dgEmployees" HorizontalAlignment= "Left" Margin= "0,0,0,0" VerticalAlignment= "Top" Height= "346" RowHeaderWidth= "0" AutoGenerateColumns= "False" CanUserAddRows= "False" CanUserResizeColumns= "False" CanUserDeleteRows= "False" ScrollViewer.CanContentScroll= "True" VerticalScrollBarVisibility= "Visible" HeadersVisibility= "Column" IsReadOnly= "True" > <DataGrid.Res

How To Set WPF DataGridCell And DataGridRow Background Color Using Triggers

Hi, There was a question on the forums on how to set the background color of DataGridCell or DataGridRow using XAML without code. I always thought that the solution will be to use code using IValueConverter. After doing some searching on MSDN and google, the answer is straightforward using Triggers. To set the color of DataGridRow, you set the DataGrid.CellStyle just below the DataGrid markup. <DataGrid Grid.Row= "0" Grid.Column= "0" AutoGenerateColumns= "False" CanUserAddRows= "False" Name= "dgStudents" > <DataGrid.CellStyle> <Style TargetType= "{x:Type DataGridCell}" > <Style.Triggers> <DataTrigger Binding= "{Binding Age}" Value= "28" > <Setter Property= "Background" Value= "Gray" ></Setter> </DataTrigger> </Style.Triggers> </Style> </DataGrid.CellStyle> <DataGrid.Columns>

Set DataGridTemplateColumn Control to ReadOnly in WPF

Image
Hi all, There was a question raised on how to set a control (ex. TextBox) inside the DataGridTemplateColumn of a WPF DataGrid to ReadOnly. The solution to that is to get the DataGridRow followed by accessing the DataGridCell and then get the VisualChild control inside the DataGridCell. By then, you can set the control's property IsReadOnly to true . XAML Code <DataGrid x:Name= "dgPerson" Grid.Column= "2" Grid.Row= "2" CanUserDeleteRows= "False" AutoGenerateColumns= "False" CanUserAddRows= "False" > <DataGrid.Columns> <DataGridTemplateColumn Header= "ID" Width= "150" > <DataGridTemplateColumn.CellTemplate> <DataTemplate > <TextBox Name= "txtID" Text= "{Binding ID, Mode=OneWay}" /> </DataTemplate> </DataGridTemplateColumn.CellTemplate> </DataGridTemplateColumn> <DataGridTemplateColumn H

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

WPF DataGrid Column Resize Event

Windows Forms DataGridView has a built-in event when the DataGridViewCell is resized but missing in WPF. To add an event for the DataGridCell when it is resized, a solution is to add an EventSetter to the DataGridCell style and then set the values of Handler and Event respectively. 1 2 3 4 5 <DataGrid.CellStyle> <Style TargetType= "DataGridCell" > <EventSetter Event= "SizeChanged" Handler= "Cell_SizedChanged" /> </Style> </DataGrid.CellStyle> In your code behind, add definition to the Handler based from the defined style. 1 2 3 4 5 private void Cell_SizedChanged( object sender, SizeChangedEventArgs e) { DataGridCell cell = (DataGridCell)sender; //TODO: Add your code here.... }

WPF Add Or Delete Rows In A DataGrid Using Observable Collection

Image
Here's a simple way of adding/deleting WPF Datagrid rows using observable collection. XAML Code 1 2 3 4 5 6 7 8 <DataGrid AutoGenerateColumns= "False" ItemsSource= "{Binding}" Margin= "12,12,12,41" Name= "dataGrid1" CanUserResizeRows= "False" CommandManager.PreviewExecuted= "UserDataGrid_PreviewDeleteCommandHandler" RowEditEnding= "dataGrid1_RowEditEnding" RowHeaderWidth= "20" > <DataGrid.Columns> <DataGridTextColumn Binding= "{Binding UserName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Header= "User name" Width= "230" > </DataGridTextColumn> <DataGridTextColumn Binding= "{Binding FirstName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Header= "First Name" Width= "245" > </DataGridTextColumn> </DataGrid.Columns> </DataGrid> C#

WPF DataGrid Row And Cells Extension Methods In VB.NET

Image
Below are extension methods in accessing WPF DataGrid Rows and Cells. These methods were based on existing C# methods and I converted them to VB.NET for VB programmers. WPF DataGrid Extension Methods: 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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 Option Explicit On Option Infer On Imports System.Runtime.CompilerServices Imports System.Windows.Controls.Primitives

How To Paint Or Set WPF DataGridCell Background Without Using IValueConverter

Here's how to paint WPF DatagridCell using extension methods without using IValueConverter. Extension Method: 1 2 3 4 5 6 7 8 9 10 11 12 Module DataGridExtensions < Extension() > Function GetRow ( ByVal grid As DataGrid, ByVal index As Integer ) As DataGridRow Dim row As DataGridRow = DirectCast (grid.ItemContainerGenerator.ContainerFromIndex(index), DataGridRow) If row Is Nothing Then grid.UpdateLayout() grid.ScrollIntoView(grid.Items(index)) row = DirectCast (grid.ItemContainerGenerator.ContainerFromIndex(index), DataGridRow) End If Return row End Function End Module MainWindow.xaml: 1 2 3 4 Dim firstRow As DataGridRow = grid1.GetRow( 0 ) 'grid1 is your DataGrid control Dim firstColumnInFirstRow As Controls.DataGridCell = DirectCast (grid1.Columns( 0 ).GetCellContent(firstRow).Parent, Controls.DataGridCell) 'set background firstColumnInFirstR

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

WPF Datagrid Paging In VB.NET Using CollectionView Class

Image
Based from the solution posted by timmyl here: How can I paginate a WPF DataGrid? . I managed to fix some bugs and added some functionalites such as MoveToFirstPage and MoveToLastPage. Paging Class 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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 Option Infer On Imports System.Collections Imports System.Collections.Generic Imports System.ComponentModel Imports System.Windows.Data Public Class PageCollectionView Inherits CollectionView Private ReadOnly _innerList As IList Private ReadOnly _itemsPerPage As Integer Private _currentPage As

Donate