The itmes in ComboBox becomes unselected in DataGrid when the IsVirtulizing is set true and the DataGrid are scrolled by scrollbar.

Nov 24, 2009 at 7:45 AM

As the title says, there is a combobox custom column in DataGrid,

when the IsVirtulizing is set to true, after the scroll of DataGrid,

there is no selected item in ComboBox.

The first item should selected in default, which is defined in the DateTemplate.

Following is the link of demo program.

http://dl.dropbox.com/u/1526548/ComboBoxInDataGrid.zip

Following is the XAML and code.

  <Window.Resources>
        <DataTemplate x:Key="myComboBox">
            <ComboBox x:Name="colEdit"
                     ItemsSource="{Binding Path=GenderList}"
                                       SelectedValuePath="[1]"
                                       DisplayMemberPath="[1]"
                                       VerticalContentAlignment="Center"
                                       SelectedIndex="0"
                                       HorizontalAlignment="Stretch">
            </ComboBox>
        </DataTemplate>
    </Window.Resources>
    <Grid Name="Grid1">
        <dg:DataGrid 
                     x:Name="DataGrid1"
                     SelectionMode="Single"
                     SelectionUnit="FullRow" 
                     AutoGenerateColumns="False"   
                     CanUserAddRows="False"
                     CanUserDeleteRows="False"
                     CanUserReorderColumns="False"
                  VirtualizingStackPanel.IsVirtualizing="True" 
                      >
            <dg:DataGrid.Columns>
                <dg:DataGridTextColumn Binding="{Binding Path=Name}" /> 
                <dg:DataGridTemplateColumn x:Name="dgColNormal"
                                           Header="Gender"
                                           Width="88.27"
                                           CellTemplate="{StaticResource myComboBox}"/>

            </dg:DataGrid.Columns>
        </dg:DataGrid>
    </Grid>

Imports System.ComponentModel
Imports System.Collections.ObjectModel

Public Class MyDataItem
    Implements INotifyPropertyChanged

    Public Event PropertyChanged As PropertyChangedEventHandler _
                 Implements INotifyPropertyChanged.PropertyChanged

    Private Sub NotifyPropertyChanged(ByVal info As String)
        Try
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(info))
        Finally

        End Try
    End Sub

    Private m_Name As String

    Private m_GenerList As List(Of String())

    Public Property Name() As String
        Get
            Return m_Name
        End Get
        Set(ByVal value As String)
            m_Name = value
        End Set
    End Property

    Public Property GenderList() As List(Of String())
        Get
            Return m_GenerList
        End Get
        Set(ByVal value As List(Of String()))

        End Set
    End Property

    Public Sub New()
        Dim unknown(1) As String
        unknown(0) = "U"
        unknown(1) = "Unknown"
        m_GenerList = New List(Of String())
        m_GenerList.Add(unknown)

        Dim Male(1) As String
        Male(0) = "M"
        Male(1) = "Male"
        m_GenerList.Add(Male)

        Dim Female(1) As String
        Female(0) = "F"
        Female(1) = "Female"
        m_GenerList.Add(Female)


    End Sub
End Class

Imports System.Collections.ObjectModel

Class Window1

    Private Sub Grid1_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles Grid1.Loaded
        Dim dataGridItemSource As New ObservableCollection(Of MyDataItem)

        For i As Integer = 0 To 100
            Dim itemData As New MyDataItem
            itemData.Name = "User " & i
            dataGridItemSource.Add(itemData)
        Next i
        Me.DataGrid1.ItemsSource = dataGridItemSource
    End Sub
End Class