如何在 MVVM .Net WPF 中使用数据集

How Use Datasets in MVVM .Net WPF

提问人:brobro 提问时间:9/18/2023 最后编辑:Craigbrobro 更新时间:9/18/2023 访问量:54

问:

我正在用 VB.net 和 WPF 开发应用程序,但我可以阅读 C# 代码 目前,我们使用 CollectionViewSource 并直接填充数据集。但对于MVVM来说,这似乎是不正确的,我认为我们花了很多时间来开发视图、事件和所有数据验证。

看看我在不使用命令中继进行点击的情况下尝试了什么 我的ModelTemplate类是一个基类,它实现了INotifyPropertyChanged: ViewXaml的:

<Grid x:Name="grid_Granulats">
    <DataGrid x:Name="datagrid_granulats" AutoGenerateColumns="False"                                            EnableRowVirtualization="True"
              ItemsSource="{Binding}"
              RowDetailsVisibilityMode="VisibleWhenSelected"
              Style="{DynamicResource DataGrid_Selection}"
              MinWidth="350"
              MinHeight="100">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Code"
                                Width="auto"
                                IsReadOnly="True"
                                Binding="{Binding Code, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged,NotifyOnValidationError=True}"/>
            <DataGridTextColumn Header="Désignation"
                                Width="auto"
                                IsReadOnly="True"
                                Binding="{Binding Designation, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged,NotifyOnValidationError=True}"/>
            <DataGridTextColumn Header="N° Silo"
                                Width="auto"
                                IsReadOnly="True"
                                Binding="{Binding Silo, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged,NotifyOnValidationError=True}"/>
            <DataGridTextColumn Header="Stock"
                                Width="auto"
                                IsReadOnly="True"
                                Binding="{Binding Stock, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged,NotifyOnValidationError=True}"/>
            <DataGridTextColumn Header="Unité"
                                Width="auto"
                                IsReadOnly="True"
                                Binding="{Binding Unite, Mode=TwoWay, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged,NotifyOnValidationError=True}"/>
        </DataGrid.Columns>
    </DataGrid>
</Grid>

查看.vb :

Public Class F_Stocks
    Public myViewModel As New StockViewModel()

    Public Sub New()
        ' Cet appel est requis par le concepteur.
        InitializeComponent()

        ' Ajoutez une initialisation quelconque après l'appel InitializeComponent().
        grid_Granulats.DataContext = myViewModel.MatListe
    End Sub

    Public Sub B_Valider_Click()
        Try
            myViewModel.addMat("N035", "Nouveau Code", 5000, 5, "T")
        Catch ex As Exception
            G_Message_Info(UC_Message, Mod_Utilitaires.Type_message.erreur, Err.Description)
        End Try
    End Sub

    Public Sub B_Supprimer_Click()
        Try
            myViewModel.removeMat(datagrid_granulats.SelectedItem)
        Catch ex As Exception
            G_Message_Info(UC_Message, Mod_Utilitaires.Type_message.erreur, Err.Description)
        End Try
    End Sub
End Class

还有我的 ViewModel,请注意,我知道该模型应该在其他类上,但这只是例如:

Imports System.Collections.ObjectModel
Imports System.Data
Imports Librairie_Parametres
    
Public Class StockViewModel
    Inherits Model_Template
    
    Public Class MatModel
        Inherits Model_Template
    
        Public Sub New(p_Code As String,
                       p_Designation As String,
                       p_Stock As Double,
                       p_silo As Integer,
                       p_unite As String)
            Code = p_Code
            Designation = p_Designation
            Stock = p_Stock
            Silo = p_silo
            Unite = p_unite
        End Sub
    
        Private m_Code As String
        Public Property Code() As String
            Get
                Return m_Code
            End Get
            Set(ByVal value As String)
                m_Code = value
                NotifyPropertyChanged("Code")
            End Set
        End Property
    
        Private m_Desgination As String
        Public Property Designation() As String
            Get
                Return m_Desgination
            End Get
            Set(ByVal value As String)
                m_Desgination = value
                NotifyPropertyChanged("Designation")
            End Set
        End Property
    
        Private m_Stock As Double
        Public Property Stock() As Double
            Get
                Return m_Stock
            End Get
            Set(ByVal value As Double)
                m_Stock = value
                NotifyPropertyChanged("Stock")
            End Set
        End Property
    
        Private m_silo As Integer
        Public Property Silo() As Integer
            Get
                Return m_silo
            End Get
            Set(ByVal value As Integer)
                m_silo = value
                NotifyPropertyChanged("Silo")
            End Set
        End Property
    
        Private m_unite As String
        Public Property Unite() As String
            Get
                Return m_unite
            End Get
            Set(ByVal value As String)
                m_unite = value
                NotifyPropertyChanged("Unite")
            End Set
        End Property
    End Class
    
    Private m_MatListe As ObservableCollection(Of MatModel)
    Public Property MatListe() As ObservableCollection(Of MatModel)
        Get
            Return m_MatListe
        End Get
        Set(ByVal value As ObservableCollection(Of MatModel))
            m_MatListe = value
            NotifyPropertyChanged("MatListe")
        End Set
    End Property
    
    Public Sub New()
        MatListe = New ObservableCollection(Of MatModel)
        MatListe.Add(New MatModel("M012", "Premier Produit", 150, 1, "kg"))
        MatListe.Add(New MatModel("M013", "Deuxieme Produit", 400, 2, "T"))
        MatListe.Add(New MatModel("M014", "Troisieme Produit", 500, 3, "kg"))
    End Sub
    
    Public Sub addMat(p_Code As String,
                      p_Designation As String,
                      p_Stock As Double,
                      p_silo As Integer,
                      p_unite As String)
    
        MatListe.Add(New MatModel(p_Code, p_Designation, p_Stock, p_silo, p_unite))
    End Sub
    
    Public Sub removeMat(mat As MatModel)
        MatListe.Remove(mat)
    End Sub
End Class

一切正常,但现在我想知道如何通过过滤从我的数据集中生成对象到我的列表?

如果需要,我可以阅读 C# 代码,这只是关于概念

C# .NET WPF vb.net MVVM

评论

0赞 Ostas 9/19/2023
“如何通过过滤从数据集中生成对象到我的列表”是什么意思?是否要对 itemsSource 应用筛选器?此外,通常不会设置 DataContext,而是将 View 的 DataContext 设置为 ViewModel,如果情况并非如此,那么您可以像这样编辑视图的 ItemsSource:grid_Granulats.DataContext = myViewModel.MatListeItemsSource="{Binding MatListe}"
0赞 Community 9/19/2023
请澄清您的具体问题或提供其他详细信息以准确说明您的需求。正如目前所写的那样,很难确切地说出你在问什么。

答: 暂无答案