MVVM Datatable到Datagrid绑定问题 [英] MVVM Datatable to Datagrid binding issue
问题描述
大家好,
所以我是VB.NET的新手,我仍然试图绕过MVVM。我认为这是设计应用程序的好方法;但是,我能够在网上追踪的只有C $示例,而且在vb.net中只有很少的Over the head信息。不幸的是,我没有足够的时间学习C $,所以将mvvm应用于vb.net充其量证明是困难的。
这是我的diellema,如果这不是正确的做事方式,请在这里忍受我,我只是想学习。我有一个数据表,我最终想要在多个窗口中的多个网格视图上显示。
我创建了一个名为DataModel的类
Hi guys,
so I am new to VB.NET and am still trying to wrap my head around MVVM. I think it is a great way of designing an application; however, all I am able to track down on the web are C$ examples and only very little "Over my head" info given in vb.net. Unfortunately I don't have enough time to learn C$ so applying mvvm to vb.net proves difficult at best.
Here is my diellema and please bear with me here if this isn't the correct way of doing things, I am just trying to learn. I have a datatable that I eventually want to show on multiple gridviews in multiple windows.
I created a class called "DataModel"
Imports System.Data
Imports System.ComponentModel
Public Class DataModel
Implements INotifyPropertyChanged
'Public Sub New(ByVal _DT As DataView)
' Me.TestDataView = _DT
'End Sub
Private _testDataView As New DataView
Public Property TestDataView() As DataView
Get
Return _testDataView
End Get
Set(value As DataView)
_testDataView = value
InvokePropertyChanged("TestDataView")
End Set
End Property
Public Sub InvokePropertyChanged(Properties As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(Properties))
End Sub
Public Event PropertyChanged(sender As Object, e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged
End Class
我目前正在Application.XAML.vb的Application Startup中添加数据
and I am currently adding data at the Application Startup in Application.XAML.vb
Imports System.Data
Class Application
' Application-level events, such as Startup, Exit, and DispatcherUnhandledException
' can be handled in this file.
Private Sub Application_Startup(sender As Object, e As StartupEventArgs) Handles Me.Startup
Dim dataTableView As DataModel
Dim dataTable As DataTable
dataTable.Columns.Add("DeptID", GetType(System.Int32))
dataTable.Columns.Add("DepartmentName", GetType(System.String))
dataTable.Columns.Add("HOD", GetType(System.String))
dataTable.Columns.Add("FacultyCount", GetType(System.String))
Dim row As DataRow = dataTable.NewRow()
row("DeptID") = 1
row("DepartmentName") = "CS&E"
row("HOD") = "John"
row("FacultyCount") = 20
dataTable.Rows.Add(row)
row = dataTable.NewRow()
row("DeptID") = 2
row("DepartmentName") = "Mech"
row("HOD") = "Bo Yo"
row("FacultyCount") = 23
dataTable.Rows.Add(row)
dataTableView.TestDataView = dataTable.DefaultView
End Sub
End Class
这是MainWindow.xaml(我只创建了一个数据网格来显示数据表,并将窗口的datacontext设置为类DataModel)
Here is the MainWindow.xaml (I only created a datagrid to display the datatable and set the datacontext of the window to the class DataModel)
<Window x:Class="MainWindow"
DataContext="DataModel"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="41*"/>
<ColumnDefinition Width="6*"/>
</Grid.ColumnDefinitions>
<DataGrid ItemsSource="{Binding TestDataView}" HorizontalAlignment="Left" Height="273" Margin="10,25,0,0" VerticalAlignment="Top" Width="480" Grid.ColumnSpan="2" />
</Grid>
</Window>
问题是,没有错误,但我看不到任何值数据网格。我真的不知道我在这里做错了什么。你们中的任何人愿意帮助我吗?非常感谢您提前
The problem is, there are no errors but I can't see any values in my datagrid. I really don't know what I am doing wrong here. Would any of you guys be willing to help me out? Thank you very much in advance
推荐答案
示例,并且在vb.net中只提供了很少的Over the head信息。不幸的是我没有足够的时间学习C
examples and only very little "Over my head" info given in vb.net. Unfortunately I don't have enough time to learn C
所以将mvvm应用到vb.net充其量证明是困难的。
这是我的diellema,如果这不是正确的做事方式,请在这里忍受我,我只是想学习。我有一个数据表,我最终想要在多个窗口中的多个网格视图上显示。
我创建了一个名为DataModel的类
so applying mvvm to vb.net proves difficult at best.
Here is my diellema and please bear with me here if this isn't the correct way of doing things, I am just trying to learn. I have a datatable that I eventually want to show on multiple gridviews in multiple windows.
I created a class called "DataModel"
Imports System.Data
Imports System.ComponentModel
Public Class DataModel
Implements INotifyPropertyChanged
'Public Sub New(ByVal _DT As DataView)
' Me.TestDataView = _DT
'End Sub
Private _testDataView As New DataView
Public Property TestDataView() As DataView
Get
Return _testDataView
End Get
Set(value As DataView)
_testDataView = value
InvokePropertyChanged("TestDataView")
End Set
End Property
Public Sub InvokePropertyChanged(Properties As String)
RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(Properties))
End Sub
Public Event PropertyChanged(sender As Object, e As PropertyChangedEventArgs) Implements INotifyPropertyChanged.PropertyChanged
End Class
我目前正在Application.XAML.vb的Application Startup中添加数据
and I am currently adding data at the Application Startup in Application.XAML.vb
Imports System.Data
Class Application
' Application-level events, such as Startup, Exit, and DispatcherUnhandledException
' can be handled in this file.
Private Sub Application_Startup(sender As Object, e As StartupEventArgs) Handles Me.Startup
Dim dataTableView As DataModel
Dim dataTable As DataTable
dataTable.Columns.Add("DeptID", GetType(System.Int32))
dataTable.Columns.Add("DepartmentName", GetType(System.String))
dataTable.Columns.Add("HOD", GetType(System.String))
dataTable.Columns.Add("FacultyCount", GetType(System.String))
Dim row As DataRow = dataTable.NewRow()
row("DeptID") = 1
row("DepartmentName") = "CS&E"
row("HOD") = "John"
row("FacultyCount") = 20
dataTable.Rows.Add(row)
row = dataTable.NewRow()
row("DeptID") = 2
row("DepartmentName") = "Mech"
row("HOD") = "Bo Yo"
row("FacultyCount") = 23
dataTable.Rows.Add(row)
dataTableView.TestDataView = dataTable.DefaultView
End Sub
End Class
这是MainWindow.xaml(我只创建了一个数据网格来显示数据表,并将窗口的datacontext设置为类DataModel)
Here is the MainWindow.xaml (I only created a datagrid to display the datatable and set the datacontext of the window to the class DataModel)
<Window x:Class="MainWindow"
DataContext="DataModel"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="41*"/>
<ColumnDefinition Width="6*"/>
</Grid.ColumnDefinitions>
<DataGrid ItemsSource="{Binding TestDataView}" HorizontalAlignment="Left" Height="273" Margin="10,25,0,0" VerticalAlignment="Top" Width="480" Grid.ColumnSpan="2" />
</Grid>
</Window>
问题是,没有错误,但我看不到任何值数据网格。我真的不知道我在这里做错了什么。你们中的任何人愿意帮助我吗?非常感谢您提前
The problem is, there are no errors but I can't see any values in my datagrid. I really don't know what I am doing wrong here. Would any of you guys be willing to help me out? Thank you very much in advance
添加模式= TwoWay从代码中设置它
Add in Mode=TwoWay to set it from the code
<datagrid itemssource="{Binding TestDataView, Mode=TwoWay" horizontalalignment="Left" height="273" margin="10,25,0,0" verticalalignment="Top" width="480" grid.columnspan="2" />
这篇关于MVVM Datatable到Datagrid绑定问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!