1

Just writing in WPF for the first time, and I ran into this situation: I'm attempting to load a combobox with values from a dataset without writing anything in codebehind (as per the MVVM way). Many things I have tried failed.

Here's the latest:

Public Class ChainTableModel
Dim _chainId As String
Dim _chainType As String

Public Property ChainId As String
    Get
        Return _chainId
    End Get
    Set(value As String)
        _chainId = value
    End Set
End Property

Public Property ChainType As String
    Get
        Return _chainType
    End Get
    Set(value As String)
        _chainType = value
    End Set
End Property

Public Sub New(chainId As String, chainType As String)
    _chainId = chainId
    _chainType = chainType
End Sub 
End Class

The ViewModel:

Public Class ChainViewModel
Implements INotifyPropertyChanged
Dim _chainTypes As ObservableCollection(Of ChainTableModel) = New ObservableCollection(Of ChainTableModel)()

Public Property ChainTypes As ObservableCollection(Of ChainTableModel)
    Get
        If _chainTypes.Count = 0 Then DBLoadChains()
        Return _chainTypes
    End Get
    Set(value As ObservableCollection(Of ChainTableModel))
        _chainTypes = value
        NotifyPropertyChanged("ChainTypes")
    End Set
End Property

Private Sub DBLoadChains()
    For Each row As DataRow In CatCollections.dsChains.Tables("ChainTable").Rows
        Dim display As String = row("Name").ToString
        Dim value As String = row("id").ToString
        If display = String.Empty Then display = value
        _chainTypes.Add(New ChainTableModel(value, display))
    Next
End Sub

Private Sub NotifyPropertyChanged(propertyName As String)
    RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub

Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged
End Class

The XAML:

<Window x:Class="ChainView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:CatLayoutTools" 
    Title="CatScript Layout Tools (WPF) - Chain Options" SizeToContent="WidthAndHeight" Height="Auto" 
    ResizeMode="NoResize" Width="Auto">

<Window.DataContext>
    <local:ChainViewModel />
</Window.DataContext>

<Window.Resources>
    <Style TargetType="GroupBox">
        <Setter Property="Margin"  Value="3,3,3,3" />
        <Setter Property="Padding"  Value="0,5,0,0" />
        <Setter Property="VerticalAlignment" Value="Top" />
    </Style>
</Window.Resources>

        <StackPanel>
            <ComboBox SelectedValuePath="id" ItemsSource="{Binding ChainTypes}" />
        </StackPanel>
</Window>

When I run the code, the combobox is populated with the following text "CatLayoutTools.ChainTableModel" the number of times as there are rows in the database query.

What am I missing here? I'm trying to accomplish the combobox loading and selecting of current item the MVVM way: Binding to a ViewModel.

Also, I have a question: as you can see, I am loading my observablecollection with data from the dataset in the viewmodel in the 'ChainTypes' property Get. Somehow I don't think this is the right place to do this, but then if I put a constructor in the viewmodel, the "" line in the XAML bugs out, telling me that object is not set to an instance of...

And if I handle the loading of ObservableCollection from my AutoCAD initialization sub, how do I preserve the ObservableCollection for when it is needed to be bound to combobox?

Any help is appreciated.

Thanks

mindofcat
  • 55
  • 1
  • 8

1 Answers1

0

You didn't specify DisplayMemberPath. Without this, result of default ToString() implementation on your type will be displayed. Also SelectedValuePath is wrong.

<ComboBox SelectedValuePath="ChainId" DisplayMemberPath="ChainType" ItemsSource="{Binding ChainTypes}" />

About initialization...you may have initialization command in your view model and then bind the Loaded event from your view to that command. More about how to bind an event here:

WPF event binding from View to ViewModel?.

The last question unfortunately I don't understand. You will store your result in _chainTypes field.

Jan Muncinsky
  • 4,282
  • 4
  • 22
  • 40