Saturday September 04 , 2010

Posts Tagged ‘Xceed’

Xceed DataGrid WPF Mouse Single Click Event

Have you been trying to find the event that fires when you select a single record from the Xceed DataGrid? It doesn’t exist! You can play around with the MouseDown event a bit, but it doesn’t quite do the trick since this fires when the scrollbar is pressed and has some other weird side effects. Other Events are worse.

Our developers (including myself) were banging our head up against the wall with this problem until we found a pretty reliable way of capturing when the user single clicks on a record on the DataGrid. I cannot answer why most WPF controls don’t have the Mouse Single Click Event, but this functionality should have been better exposed since it has so many useful applications. This one is a gem, so please comment if you found this article useful in your project.

Enjoy!

Private Sub myDataGridControl_PropertyChanged _
(ByVal sender As Object, _
ByVal e As System.ComponentModel.PropertyChangedEventArgs)
Handles myDataGridControl.PropertyChanged

    If e.PropertyName = "GlobalSelectedItems" Then
        'myDataGridControl.SelectedItem was clicked!
    End If

End Sub
 

Xceed DataGrid WPF Object Data Binding

Many of the examples that are provided for the Xceed DataGrid involve binding directly to a database table source, so I wanted to give an example of how easy it is to bind to an Object. Xceed DataGrid makes excellent use of the WPF binding and thus only needs it’s ItemsSource property to be set to a data source that implements IEnumerable.

Guess what? By default, all Generic Lists in the .Net framework are compatible data sources. To show how easy it is to bind the Xceed datagrid to a Generic List of Objects, I have created a sample project (which you can download) that does essentially 3 things:

1. Declares a class named: MyRecord with 4 public properties (First, Last, Title and Phone).
2. Defines a function that creates a Generic List of MyRecord objects, sets temp data for each object and returns the new Generic List of MyRecord objects.
3. In the New() method of the Window that contains the DataGrid, we simply set the ItemsSource to the Generic List of MyRecord objects.

Enjoy!

Class Window1 

    Public Sub New()

        InitializeComponent()

        DataGridControl1.ItemsSource = GetMyRecords()

    End Sub

    Private Function GetMyRecords() As List(Of MyRecord)
        Dim MyRecords As New List(Of MyRecord)

        Dim _myRecord As MyRecord

        _myRecord = New MyRecord
        _myRecord.First = "Carlos"
        _myRecord.Last = "Gonzalez"
        _myRecord.Title = "Managing Director"
        _myRecord.Phone = "213-555-1212"
        MyRecords.Add(_myRecord)

        _myRecord = New MyRecord
        _myRecord.First = "Vivien"
        _myRecord.Last = "Dracon"
        _myRecord.Title = "Managing Director"
        _myRecord.Phone = "213-777-1212"
        MyRecords.Add(_myRecord)

        _myRecord = New MyRecord
        _myRecord.First = "John"
        _myRecord.Last = "Doe"
        _myRecord.Title = "Developer"
        _myRecord.Phone = "213-111-1212"
        MyRecords.Add(_myRecord)

        _myRecord = New MyRecord
        _myRecord.First = "Jane"
        _myRecord.Last = "Doe"
        _myRecord.Title = "Sales"
        _myRecord.Phone = "213-999-1212"
        MyRecords.Add(_myRecord)

        _myRecord = New MyRecord
        _myRecord.First = "Bill"
        _myRecord.Last = "Gates"
        _myRecord.Title = "Retired"
        _myRecord.Phone = "213-222-1212"
        MyRecords.Add(_myRecord)

        Return MyRecords
    End Function

    Private Class MyRecord
        Private _first As String
        Private _last As String
        Private _title As String
        Private _phone As String

        Public Property First()
            Get
                Return _first
            End Get
            Set(ByVal value)
                _first = value
            End Set
        End Property

        Public Property Last()
            Get
                Return _last
            End Get
            Set(ByVal value)
                _last = value
            End Set
        End Property

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

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

End Class