乐筑天下

搜索
欢迎各位开发者和用户入驻本平台 尊重版权,从我做起,拒绝盗版,拒绝倒卖 签到、发布资源、邀请好友注册,可以获得银币 请注意保管好自己的密码,避免账户资金被盗
查看: 56|回复: 3

Load Combobox from DataSet using VB.Net MVVM (WPF)

[复制链接]

4

主题

11

帖子

1

银币

初来乍到

Rank: 1

铜币
27
发表于 2017-8-4 19:48:12 | 显示全部楼层 |阅读模式
Hi All,
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:
The Model:
[ol]
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

[/ol]
The ViewModel:
[ol]
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
[/ol]
The XAML:
[ol]
        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">

   
        
   

   
        
            
            
            
        
   

            
               
            
[/ol]
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,
回复

使用道具 举报

4

主题

11

帖子

1

银币

初来乍到

Rank: 1

铜币
27
发表于 2017-8-5 15:19:49 | 显示全部楼层
SOLVED.
Was missing SelectedValuePath and DisplayMemberPath in the XAML. This solved it for me:
回复

使用道具 举报

24

主题

204

帖子

6

银币

后起之秀

Rank: 20Rank: 20Rank: 20Rank: 20

铜币
300
发表于 2017-8-8 07:20:17 | 显示全部楼层

Take the construction of your ViewModel out of XAML and put it into the constructor of your UserControl/Window in the code behind.  
1. This will let you manipulate the ViewModel before XAML components are Initialized.
2. This will set you up for Dependency Injection in the future.
回复

使用道具 举报

4

主题

11

帖子

1

银币

初来乍到

Rank: 1

铜币
27
发表于 2017-8-14 22:22:43 | 显示全部楼层
You're right. Taking the constructor of the view model out of the XAML and moving it to code behind helped tremendously. I was able to handle my observable collections initialization there; a lot cleaner too! Thanks.
回复

使用道具 举报

发表回复

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

  • 微信公众平台

  • 扫描访问手机版

  • 点击图片下载手机App

QQ|关于我们|小黑屋|乐筑天下 繁体中文

GMT+8, 2025-2-4 12:59 , Processed in 0.203766 second(s), 60 queries .

© 2020-2025 乐筑天下

联系客服 关注微信 帮助中心 下载APP 返回顶部 返回列表