VIBlend
Home Products Downloads Purchase Support Forum About Blog

VIBlend WPF DateTimeEditor and Data binding

by viblend 5. June 2010 08:58

This blog post illustrates how you can use the VIBlend WPF DateTimeEditor control in data binding scenarios.

The following is a complete sample, ready to run once you have copied it to your project.

At first, create a new WPF application and add a reference to the VIBlendWPFEditors.dll. Then add a new DateTimeEditor and ListBox controls in your application’s window. After that, bind the DateTimeEditor’s Value as well as the ListBox’s SelectedValue to a property named DateTimeValue.

XAML
<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:viblend="clr-namespace:VIBlend.WPF.Controls;assembly=VIBlendWPFEditors"
        xmlns:local="clr-namespace:WpfApplication1"
        Title="MainWindow" Height="200" Width="300">
    <Grid x:Name="LayoutRoot" Height="200" Width="300">
        <Grid.ColumnDefinitions>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="30"></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <viblend:DateTimeEditor Grid.Row="1" Value="{Binding DateTimeValue, Mode=TwoWay}" VerticalAlignment="Top" HorizontalAlignment="Left" Width="100" Height="20"></viblend:DateTimeEditor>
         <ListBox Grid.Column="1" Grid.Row="1" SelectedValue="{Binding DateTimeValue, Mode=TwoWay}" Height="110" HorizontalAlignment="Left"  Name="listBox1" VerticalAlignment="Top" Width="130"/>
        <Label Content="DateTime Items" Grid.Column="1" Height="28" HorizontalAlignment="Left" Name="label1" VerticalAlignment="Top" />
        <Label Content="DateTime Editor" Height="28" HorizontalAlignment="Left" Name="label2" VerticalAlignment="Top" />
    </Grid>
</Window>

The next step is to create a class which defines the DateTimeValue property.    

        public class DataBindingData : INotifyPropertyChanged
        {    
            private DateTime dateTime = new DateTime(2010, 06, 06);
            public DataBindingData()
            {
            }

            public DateTime DateTimeValue
            {
                get
                {
                    return this.dateTime;
                }
                set
                {
                    this.dateTime = value;
                    this.OnPropertyChanged("DateTimeValue");
                }
            }

            public event PropertyChangedEventHandler PropertyChanged;
            protected virtual void OnPropertyChanged(string name)
            {
                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs(name));
                }
            }
      }


The final step is to fill the ListBox with DateTime items and to set the DataContext property of the LayoutRoot object. This is implemented in the MainWindow’s constructor, after the InitializeComponent call.           

            this.LayoutRoot.DataContext = new DataBindingData();
            List<DateTime> dateTimeList = new List<DateTime>();
            dateTimeList.Add(new DateTime(2010, 06, 06));
            dateTimeList.Add(new DateTime(2010, 06, 07));
            dateTimeList.Add(new DateTime(2010, 06, 08));
            dateTimeList.Add(new DateTime(2010, 06, 09));
            dateTimeList.Add(new DateTime(2010, 06, 10));
            this.listBox1.ItemsSource = dateTimeList;


The image below represents our sample’s main window. When you select a value from the ListBox, the DateTimeEditor’s Value will be changed and vice versa.

VIBlend DataGridView for WinForms - Data Binding Example

by viblend 25. April 2009 16:43

We're frequently asked about the easiest way to bind the WinForms data grid control to a data source. When you install VIBlend WinForms Controls the setup installs a folder with several fully functional demo projects in VB.NET and C#. You can learn a lot by browsing through this code base.

In any case here's a very simple data binding example where we create a List of objects and use it as a data source:

C# Example:

using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using VIBlend.WinForms.Controls;
using VIBlend.WinForms.DataGridView;
using VIBlend.Utilities;

namespace DataBindingDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            List<Employee> list = new List<Employee>();
            for (int i = 0; i < 100; i++)
                list.Add(new Employee("Name " + i, "Title " + i, "Phone " + i, "CellPhone " + i, "Address " + i));

            dataGrid.VIBlendTheme = VIBlend.Utilities.VIBLEND_THEME.OFFICEBLACK;

            dataGrid.BoundFields.Add(new BoundField("Employee Name", "Name"));
            dataGrid.BoundFields.Add(new BoundField("Employee Title", "Title"));
            dataGrid.BoundFields.Add(new BoundField("Employee Phone", "Phone"));
            dataGrid.BoundFields.Add(new BoundField("Employee Cell Phone", "CellPhone"));
            dataGrid.BoundFields.Add(new BoundField("Employee Address", "Address"));

            dataGrid.DataSource = list;
            dataGrid.DataBind();

            dataGrid.RowsHierarchy.AutoResize();
            dataGrid.ColumnsHierarchy.AutoResize();
            dataGrid.Refresh();
        }


    }

    class Employee
    {
        public Employee(string Name, string Title, string Phone, string CellPhone, string Address)
        {
            this.Name = Name;
            this.Title = Title;
            this.Phone = Phone;
            this.CellPhone = CellPhone;
            this.Address = Address;
        }

        #region Private Members
        private string _name;
        private string _title;
        private string _phone;
        private string _cellphone;
        private string _address;
        #endregion

        #region Properties
        public string Name
        {
            get { return _name; }
            set { _name = value; }
        }

        public string Title
        {
            get { return _title; }
            set { _title = value; }
        }

        public string Phone
        {
            get { return _phone; }
            set { _phone = value; }
        }

        public string CellPhone
        {
            get { return _cellphone; }
            set { _cellphone = value; }
        }

        public string Address
        {
            get { return _address; }
            set { _address = value; }
        }

        #endregion
    }

VB.NET Example:

Imports System
Imports System.Collections.Generic
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms
Imports VIBlend.WinForms.Controls
Imports VIBlend.WinForms.DataGridView
Imports VIBlend.Utilities

Namespace DataBindingDemo
    Partial Public Class Form1
        Inherits Form
        Public Sub New()
            InitializeComponent()
        End Sub

        Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
            Dim list As New List(Of Employee)()
            For i As Integer = 0 To 99
                list.Add(New Employee("Name " & i, "Title " & i, "Phone " & i, "CellPhone " & i, "Address " & i))
            Next i

            dataGrid.VIBlendTheme = VIBlend.Utilities.VIBLEND_THEME.OFFICEBLACK

            dataGrid.BoundFields.Add(New BoundField("Employee Name", "Name"))
            dataGrid.BoundFields.Add(New BoundField("Employee Title", "Title"))
            dataGrid.BoundFields.Add(New BoundField("Employee Phone", "Phone"))
            dataGrid.BoundFields.Add(New BoundField("Employee Cell Phone", "CellPhone"))
            dataGrid.BoundFields.Add(New BoundField("Employee Address", "Address"))

            dataGrid.DataSource = list
            dataGrid.DataBind()

            dataGrid.RowsHierarchy.AutoResize()
            dataGrid.ColumnsHierarchy.AutoResize()
            dataGrid.Refresh()
        End Sub


    End Class

    Friend Class Employee
        Public Sub New(ByVal Name As String, ByVal Title As String, ByVal Phone As String, ByVal CellPhone As String, ByVal Address As String)
            Me.Name = Name
            Me.Title = Title
            Me.Phone = Phone
            Me.CellPhone = CellPhone
            Me.Address = Address
        End Sub

        #Region "Private Members"
        Private _name As String
        Private _title As String
        Private _phone As String
        Private _cellphone As String
        Private _address As String
        #End Region

        #Region "Properties"
        Public Property Name() As String
            Get
                Return _name
            End Get
            Set(ByVal value As String)
                _name = value
            End Set
        End Property

        Public Property Title() As String
            Get
                Return _title
            End Get
            Set(ByVal value As String)
                _title = value
            End Set
        End Property

        Public Property Phone() As String
            Get
                Return _phone
            End Get
            Set(ByVal value As String)
                _phone = value
            End Set
        End Property

        Public Property CellPhone() As String
            Get
                Return _cellphone
            End Get
            Set(ByVal value As String)
                _cellphone = value
            End Set
        End Property

        Public Property Address() As String
            Get
                Return _address
            End Get
            Set(ByVal value As String)
                _address = value
            End Set
        End Property

        #End Region
    End Class
End Namespace

About the author

Some text that describes me

Tag cloud

Recent comments

Comment RSS
Copyright © 2011 VIBlend  
ALL RIGHTS RESERVED  
 
Terms of Use | Privacy Policy
WinForms Controls Purchase Online About Us
       
DataGrid Navigation Pane Technical Support Blog
ScrollBar TreeView
ListBox ProgressBar Downloads Register
ComboBox Buttons
TabControl Editors Documentation Client Login

VIBlend Blog is powered by BlogEngine.NET