尝试使用数据集从数据库加载组合框;项目不会显示在组合框中。尝试在不编写任何代码的情况下执行此操作。
型号:
Imports System.Data.OleDb
Public Class ChainModel
Public Property ChainId As String
Public Property ChainType As String
Public Sub New(chainId As String, chainType As String)
Me.ChainId = chainId
Me.ChainType = chainType
End Sub
End Class
数据集填充:
Public Class DBCollection
Public Shared dsChains As DataSet
Public Shared Sub FillDataSet()
Try
dsChains = New DataSet()
Dim adpt As OleDbDataAdapter = New OleDbDataAdapter With {
.SelectCommand = New OleDbCommand("SELECT * FROM Chain ORDER BY Name, Manufacturer;", DbConn)
}
adpt.Fill(dsChains, Tbl)
adpt.Dispose()
Catch ex As Autodesk.AutoCAD.Runtime.Exception
MsgBox("Unable to populate dataset! " & vbCrLf & ex.Message.ToString)
End Try
End Sub
End Class
ViewModel:
Public Class TTStraightViewModel
Implements INotifyPropertyChanged
Private Sub NotifyPropertyChanged(Optional propertyName As String = "")
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
Public Event PropertyChanged As PropertyChangedEventHandler Implements
INotifyPropertyChanged.PropertyChanged
Dim _chainTypes As ObservableCollection(Of ChainModel) = New ObservableCollection(Of ChainModel)()
Public Sub New()
End Sub
Public Property ChainTypes As ObservableCollection(Of ChainModel)
Get
If _chainTypes.Count = 0 Then DBLoadChains()
Return _chainTypes
End Get
Set(value As ObservableCollection(Of ChainModel))
_chainTypes = value
NotifyPropertyChanged("ChainTypes")
End Set
End Property
Private Sub DBLoadChains()
For Each row As DataRow In DBCollection.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 ChainModel(value, display))
Next
End Sub
End Class
XAML:
<Window x:Class="TtStraightView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace: LayoutTools" ResizeMode="NoResize" Width="Auto"
xmlns:p="clr-namespace:LayoutTools.My"
Title="Table Top Conveyor Straight" SizeToContent="WidthAndHeight" Height="Auto">
<Window.DataContext>
<local:TTStraightViewModel />
</Window.DataContext>
<Window.Resources>
<p:MySettings x:Key="MySettings" />
</Window.Resources>
<StackPanel>
<ComboBox Margin="0,3,0,3" Grid.Column="2" Grid.Row="1" Width="230" SelectedValuePath="ChainId" DisplayMemberPath="ChainType" ItemsSource="{Binding ChainTypes}"/>
</StackPanel>
</Window>
DBCollection。在程序启动期间调用FillDataSet((。
几天来,我一直在努力解决这个问题,研究,以前的代码等等,但仍然不明白为什么当我运行代码时,组合框没有显示任何项目。有什么想法吗?
最终解决了我的这种棘手情况,对于任何在VB.NET中工作的人来说,如果他们可能对如何以MvvM的方式(没有任何代码隐藏(用数据库数据填充组合框感兴趣,下面是我如何完成的。
1( 根据需要创建一个类来模拟数据库表的属性:
Public Class ChainModel
Public Property ChainId As Integer
Public Property ChainType As String
Public Sub New(chainId As Integer, chainType As String)
Me.ChainId = chainId
Me.ChainType = chainType
End Sub
End Class
2( 创建一个数据库类来填充要使用的数据集(请确保在此步骤之前设置好数据库连接(:
Public Class DBCollection
Public Shared dsChains As DataSet
Public Shared Sub FillDataSet()
dsChains = New DataSet()
Dim adpt As OleDbDataAdapter = New OleDbDataAdapter With {
.SelectCommand = New OleDbCommand("SELECT id,Name FROM Chain ORDER BY Name, Manufacturer;", Cat.DbConn)
}
adpt.Fill(dsChains,"Chain")
adpt.Dispose()
End Sub
End Class
3( 设置MvvM模型类,以便在构造函数中填充数据集,并直接从数据集填充模型的可观察集合属性:
Public Class TTStraightModel
Dim _chainType As String = My.Settings.TTStraightChainType
Dim _chainTypeList As ObservableCollection(Of ChainModel)
Public Sub New()
DBCollection.FillDataSet()
End Sub
Public Property ChainType As String
Get
Return _chainType
End Get
Set(value As String)
_chainType = value
My.Settings.TTStraightChainType = _chainType
End Set
End Property
Public Property ChainTypeList As ObservableCollection(Of ChainModel)
Get
_chainTypeList = New ObservableCollection(Of ChainModel)()
For Each row As DataRow In DBCollection.dsChains.Tables("Chain").Rows
Dim display As String = row("Name").ToString
Dim value As String = row("id").ToString
If display = String.Empty Then display = value
_chainTypeList.Add(New ChainModel(value, display))
Next
Return _chainTypeList
End Get
Set(value As ObservableCollection(Of ChainModel))
_chainTypeList = value
End Set
End Property
End Class
4( 设置MvvM视图模型,使其将模型的可观察集合属性读取为自己的可观察收集属性:
Public Class TTStraightViewModel
Implements INotifyPropertyChanged
Private Sub NotifyPropertyChanged(Optional propertyName As String = "")
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub
Public Event PropertyChanged As PropertyChangedEventHandler Implements
INotifyPropertyChanged.PropertyChanged
ReadOnly _ttStraightModel As New TTStraightModel()
Public Sub New()
End Sub
Public Property ChainType As String
Get
Return _ttStraightModel.ChainType
End Get
Set(value As String)
_ttStraightModel.ChainType = value
NotifyPropertyChanged("ChainType")
End Set
End Property
Public Property ChainTypeList As ObservableCollection(Of ChainModel)
Get
Return _ttStraightModel.ChainTypeList
End Get
Set(value As ObservableCollection(Of ChainModel))
_ttStraightModel.ChainTypeList = value
NotifyPropertyChanged("ChainTypeList")
End Set
End Property
End Class
5( 最后,设置MvvM XAML视图,使组合框直接绑定到视图模型的公开属性(在首先将视图模型设置为视图的数据上下文之后(:
<Window x:Class="TtStraightView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:CatScriptLayoutTools" ResizeMode="NoResize" Width="Auto"
xmlns:p="clr-namespace:CatScriptLayoutTools.My"
Title="Table Top Conveyor Straight" SizeToContent="WidthAndHeight" Height="Auto">
<Window.DataContext>
<local:TTStraightViewModel />
</Window.DataContext>
<Window.Resources>
<p:MySettings x:Key="MySettings" />
</Window.Resources>
<StackPanel>
<ComboBox Margin="0,3,0,3" Width="230" SelectedValuePath="ChainId" DisplayMemberPath="ChainType" ItemsSource="{Binding ChainTypeList}" SelectedItem="{Binding ChainType}"/>
</StackPanel>
</Window>
SelectedValuePath="ChainId"DisplayMemberPath="ChainType"确保将正确的id/值组合绑定到组合框。