所以,我的设置是这样的:
TabControl
,有5个标签。为了保持我的文件中的代码干净,我决定将5个选项卡的内容分为5 UserControls
。UserControl
客户(查看:CustomersView,ViewModel:CustomersViewModel),ComboBox
。 ComboBox ItemSource
绑定到由ObservableCollection(Of String)
填充的DbServiceKlant.GetKlantenlijst
。Sub
来帮助数据到数据库。我首先在MainWindow中完成了这一切,但代码采用寿司比例(在.xaml和.xaml.vb中),它不再可维护。将所有内容都放在1个文件中,可以很容易地传递变量并将项添加到ObservableCollection中。
随着UserControls中所有内容的分离,对我来说一直是个问题,我将如何实现这一目标。基本上是:
Customers
有一个ObservableCollection
填充Customer
和一个用于打开UserControl AddCustomer
的按钮。现在的问题是:
当所有内容都在1个文件中时,我只是操纵了AddCustomer的Visibility
并且可以更新ObservableCollection。
这是Customers
的摘录。
<ComboBox Background="White" ItemsSource="{Binding Klantenlijst}" SelectedItem="{Binding SelectedKlant}" Grid.Column="1" Grid.Row="0" Grid.ColumnSpan="4" />
<Button Grid.Column="5" Command="{Binding OpenAddKlant}" ToolTip="Nieuwe klant">
<Image Source="/Images/Add.png" />
</Button>
ObservableCollection。
Private _oKlantenlijst As ObservableCollection(Of String) = DbServiceKlant.GetKlantenlijst
Public Property Klantenlijst As ObservableCollection(Of String)
Get
Return _oKlantenlijst
End Get
Set(ByVal value As ObservableCollection(Of String))
_oKlantenlijst = value
OnPropertyChanged()
End Set
End Property
ICommand:
Private _oOpenAddKlant As New RelayCommand(AddressOf OpenAddKlantSub)
Public ReadOnly Property OpenAddKlant As RelayCommand
Get
Return _oOpenAddKlant
End Get
End Property
实际的Sub
Public Sub OpenAddKlantSub()
' I think here should be the code to open the AddCustomer UserControl
End Sub
在我的1文件应用程序(我正在转换为UserControls)中,AddCustomer弹出窗口中有Sub
Private Sub PopUpAddKlantAdd(sender As Object, e As RoutedEventArgs)
Try
If PupAddKlaNaa.Equals("") OrElse PupAddKlaStr.Equals("") OrElse PupAddKlaGem.Equals("") Then
MessageBox.Show("U moet minstens de naam, straat en gemeente invullen om een nieuwe klant aan te kunnen maken.", "Geen gegevens", MessageBoxButton.OK, MessageBoxImage.Information)
Else
DbServiceKlant.Create(PupAddKlaNaa, PupAddKlaStr, PupAddKlaNr_, PupAddKlaBus, PupAddKlaPos, PupAddKlaGem, PupAddKlaLan, PupAddKlaTel, PupAddKlaBTW)
_oKlant = DbServiceKlant.GetDetails(PupAddKlaNaa)
PopUpAddKlantClose()
PopUpAddKlantClear()
VoGetNaamKlanten()
VoFillDetailsKlant(_oKlant)
VoKlaSel = _oKlant.Naam
End If
Catch ex As Exception
ExceptionLogger.Log(ex, 1)
End Try
End Sub