locked
How do I property bind this ComboBox? RRS feed

  • Question

  • I'm having trouble databinding here. This is more of a silverlight question even though I'm using a Telerik Grid that has two columns. I bind rows to the GridView but only have data for the first column. I want the user to select values from comboboxes in the second column. The Display values show up properly but the values come back as my class name instead of the actual value. Can't figure out now to have the comboboxes give me the actual values, I feel I'm not binding it right. Instead of the value of Import ImportDataFieldName I get "WCFService.ImportSystemFields" actual class name

    <telerikGridView:RadGridView>
    x:Name="gvHeaders"
    ShowGroupPanel="False"
    Margin="-72,145,-2,54"
    CanUserReorderColumns="False"
    CanUserInsertRows="False"
    CanUserSortColumns="False"
    HorizontalAlignment="Left"
    AutoGenerateColumns="False"
    IsReadOnly="False"
    Foreground="Black"
    CanUserSelect="True" Background="{x:Null}" Width="500" >
    <telerikGridView:RadGridView.Columns>
    <telerikGridView:GridViewDataColumn
    Header="Your Import Headers"
    UniqueName="ImportHeaders"
    IsReadOnly="True"
    Background="{x:Null}"
    DataMemberBinding="{Binding HeaderField}" />
    <telerikGridView:GridViewDataColumn Header="System Fields" UniqueName="SystemFields">
    <telerikGridView:GridViewColumn.CellTemplate>
    <DataTemplate>
    <ComboBox x:Name="cmbTestCombo"
    SelectedItem="{Binding Path=ImportDataFieldName, Mode=TwoWay}"
    Loaded="SetDataForCombo"
    DisplayMemberPath="DisplayName"
    Height="24" Width="120">
    </ComboBox>
    </DataTemplate>
    </telerikGridView:GridViewColumn.CellTemplate>
    </telerikGridView:GridViewDataColumn>
    </telerikGridView:RadGridView.Columns>
    </telerikGridView:RadGridView>

    My Class that binds to the Gridview
    Public Class ImportHeaderData
    Implements INotifyPropertyChanged

    Private _headerfield As String
    Public Property HeaderField() As String
    Get
    Return _headerfield
    End Get
    Set(ByVal Value As String)
    _headerfield = Value
    FirePropertyChange("HeaderField")
    End Set
    End Property

    Private _importDataFieldName As String
    Public Property ImportDataFieldName() As String
    Get
    Return _importDataFieldName
    End Get
    Set(ByVal value As String)
    _importDataFieldName = value
    FirePropertyChange("ImportDataFieldName")
    End Set
    End Property

    Public Shared Function GetData(ByVal SourceString As String()) As List(Of ImportHeaderData)
    Dim Source As New List(Of ImportHeaderData)
    Dim oData As ImportHeaderData
    For Each Item As String In SourceString
    oData = New ImportHeaderData
    oData.HeaderField = Item
    oData.ImportDataFieldName = "UserRequestID"
    Source.Add(oData)
    Next
    oData = Nothing
    Return Source
    End Function

    Private Sub FirePropertyChange(ByVal PropertyName As String)
    RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(PropertyName))
    End Sub

    Public Event PropertyChanged(ByVal sender As Object, ByVal e As System.ComponentModel.PropertyChangedEventArgs)
    Implements System.ComponentModel.INotifyPropertyChanged.PropertyChanged

    End Class

    I bind it to grid on new()
    Public Sub New()
    InitializeComponent()
    gvHeaders.ItemsSource = ImportHeaderData.GetData("rowvalue1,value2,value4".Split(","))
    End Sub

    then on Combobox load I bind this class.

    Public Shared DestinationAreaSystemFields As ObservableCollection(Of WCFService.ImportSystemFields)


    Private Sub SetDataForCombo(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs)

    CType(sender, System.Windows.Controls.ComboBox).ItemsSource = Helper.DestinationAreaSystemFields

    End Sub

    <DataContract()> _
    Public Class ImportSystemFields

    Private _DataFieldName As String
    <DataMember()> _
    Public Property DataFieldName() As String
    Get
    Return _DataFieldName
    End Get
    Set(ByVal Value As String)
    _DataFieldName = Value
    End Set
    End Property


    Private _displayName As String
    <DataMember()> _
    Public Property DisplayName() As String
    Get
    Return _displayName
    End Get
    Set(ByVal Value As String)
    _displayName = Value
    End Set
    End Property

    End Class
    Thursday, December 10, 2009 4:03 PM

Answers

  •  Set the DisplayMemberPath property of the ComboBox to the property you want to get the value from that should be displayed in the combobox, our override the ToString() method of the ImportSystemFields and make sure it returns the default value you want to display in the ComboBox. By default when not DisplayMemberPath is specified or an ItemTemplate, is that the ComboBox will call the ToString of the items bound to the combobox.

    Thursday, December 10, 2009 5:22 PM