Posts

Showing posts with the label XAML

Donate

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#

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

Add Checkbox In WPF Datagrid DatagridTemplateColumnHeader (For Checkall)

Image
Hello, Here's how to add checkbox in WPF Datagrid DatagridTemplateColumn to simulate checkall. I have included the TSQL script, XAML and C# codes. Perform these steps below 1. Create an MSSQL database with the following Product Table schema below: Table: Products Fields: -    ProductID (int, autoincrement) -    ProductName (varchar) -    UnitPrice (decimal) -    QuantityPerUnit (varchar) -    Discontinue (bit)  SQL Script: USE [Your_Database_Name] GO /****** Object: Table [dbo].[Products] Script Date: 05/27/2013 14:07:39 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[Products]( [ProductID] [int] NOT NULL , [ProductName] [varchar](50) NULL , [UnitPrice] [decimal](18, 2) NULL , [QuantityPerUnit] [varchar](50) NULL , [Discontinue] [bit] NULL , CONSTRAINT [PK_Products] PRIMARY KEY CLUSTERED ( [productID] ASC ) WITH

Adding An Item to WPF Datagrid (C#)

Hello! VB.NET Version: Adding An Item to WPF Datagrid (VB.NET) Here's the C# version of how to add an item manually to a WPF DataGrid. DataGridTextColumn c1 = new DataGridTextColumn(); c1.Header = "Test" ; c1.Binding = new Binding( "test" ); c1.Width = 473; dataGrid.Columns.Add(c1); dataGrid.Items.Add( new Names() {test= "just testing" }); Class property: public string test { get ; set ; } XAML code: <DataGrid AutoGenerateColumns= "False" Height= "289" HorizontalAlignment= "Left" Margin= "10,10,0,0" Name= "dataGrid" VerticalAlignment= "Top" Width= "481" Grid.ColumnSpan= "2" ItemsSource= "{Binding }" > </DataGrid>

Add An Item To WPF Datagrid (VB.NET)

Hello! C# Version: Adding An Item to WPF Datagrid (C#) There was a question on visual basic forum on how to add an item to a wpf datagrid. Here's a simple example below. Code view: Dim linkColumn As New DataGridTextColumn : Dim titleColumn As New DataGridTextColumn linkColumn.Header = "Links:" : titleColumn.Header = "Titles:" linkColumn.Width = dataGrid.Width / 2 : titleColumn.Width = dataGrid.Width / 2 linkColumn.Binding = New System.Windows.Data.Binding( "Link" ) : titleColumn.Binding = New System.Windows.Data.Binding( "Title" ) dataGrid.Columns.Add(linkColumn) : dataGrid.Columns.Add(titleColumn) dataGrid.Items.Add( New DataItems() With {.Link = "http://testingme.com" , .Title = "mytesting" }) XAML: <DataGrid AutoGenerateColumns= "False" Height= "289" HorizontalAlignment= "Left" Margin= "10,10,0,0" Name= "dataGrid" Ve

WPF TimePicker Control

Image
Based from these sources:  a. (http://jobijoy.blogspot.com.au/2007/10/time-picker-user-control.html)  b. Dipitimaya Patra's datepicker in Datagrid I came up with a modified custom timepicker control embedded in WPF Datagrid with databinding features. I encountered bugs on migrating the present time picker custom control throughout development in .Visual Studio 2010 and I manage to fix them myself. The errors are specifically found on the keydown events and time updates (increment/decrement of values). Here are the things that I did to make this work: 1. I added a dependency property to the user control public DateTime TimeValue { get { return (DateTime)GetValue(TimeProperty); } set { SetValue(TimeProperty, value ); } } public static readonly DependencyProperty TimeProperty = DependencyProperty.Register( "TimeValue" , typeof (DateTime), typeof (TimeControlBinding), new UIPropertyMetadata(DateTi

Add Reference To WPF Merged Resource Dictionary In Window.xaml

Good evening guys! Here's how to add reference to WPF Merged Dictionary in Window.xaml. <Window.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <!--resource dictionary is placed in folder--> <ResourceDictionary Source= "/ResourceDictionaries/RoundedButton.xaml" /> <!--dictionary is inside WPF project--> <!--<ResourceDictionary Source="RoundedButton.xaml" /> --> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Window.Resources>

Simple WPF Datagrid Demo Or Tutorial

I have developed a project on WPF before. WPF has been utilized in Vista OS and other software. But this cannot replace the stability of Windows Forms. Still, I don't have ample time to dive deeply in WPF, hopefully soon! LOL.. :D So, to begin with: 1. Download and install WPF Toolkit (Visual Studio 2008) 2. Open Visual Studio 2008 3. Add a WPF Project (Just call it GridDemo) 4. Add a DataGrid in your Window.xaml 5. Make sure to have referenced wpf toolkit: http://schemas.microsoft.com/wpf/2008/toolkit/ 6. Customize Your Grid <grid:DataGrid AutoGenerateColumns= "False" Margin= "8,11,10,78" ItemsSource= "{Binding}" Name= "CusGrid" > <grid:DataGrid.Columns> <grid:DataGridTextColumn Binding= "{Binding Path=CustomerID}" Header= "Customer ID" /> </grid:DataGrid.Columns> ....other columns goes here </grid:DataGrid> 7. In your Window1.cs file, populate your gr

Donate