如何绑定Observable Collection并保存到数据库

发布于 2025-01-01 17:28:30 字数 940 浏览 0 评论 0原文

我很快就会做一个 MVVM 项目,并且正在编写一些教程/示例。如何获取以下代码并将其连接到数据库。如果我有一个数据网格,如何更改数据网格中的信息并使其自动更新?我将使用 MS SQL。感谢您的任何提示或建议。

Class Person 

  Property _name As Integer
Property Name As Integer
    Get
        Return _name
    End Get
    Set(value As Integer)
        _name = value
        RaisePropertyChanged("Name")

    End Set
End Property
End class

然后在另一个类中:

Class Collections

Public namelist As New ObservableCollection(Of Person)

    namelist.Add(New Person With {.Name = Nothing}) 

--- 然后在 XAML 中

    <ObjectDataProvider x:Key="test" ObjectType="{x:Type local:Collections}"

     <DataGrid ItemsSource="{Binding Source= {StaticResource test}}">

      <DataGridTextColumn Header="Name" Binding="{Binding Sample, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

  ect...ect....ect...

现在我在哪里或如何将我的 MS SQL 表连接到所有这些?

I will be doing an MVVM Project soon and I'm working a couple tutorial/examples out. How do I take the following code and connect it to a database. If I were to have a datagrid, how can I change information in the datagrid and have it automatically update? I'll be using MS SQL. Thanks for any tips or advice.

Class Person 

  Property _name As Integer
Property Name As Integer
    Get
        Return _name
    End Get
    Set(value As Integer)
        _name = value
        RaisePropertyChanged("Name")

    End Set
End Property
End class

Then in another class:

Class Collections

Public namelist As New ObservableCollection(Of Person)

    namelist.Add(New Person With {.Name = Nothing}) 

--- Then in XAML

    <ObjectDataProvider x:Key="test" ObjectType="{x:Type local:Collections}"

     <DataGrid ItemsSource="{Binding Source= {StaticResource test}}">

      <DataGridTextColumn Header="Name" Binding="{Binding Sample, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

  ect...ect....ect...

Now where or how do I connect my MS SQL table to all this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

天暗了我发光 2025-01-08 17:28:30

如果您使用 MVVM,那么您的绑定(如果连接正确)也会将您在 GridView 中所做的更改传播到其绑定的属性(如果您在单元格编辑完成时连接事件等)。

以下是相关来源:

< a href="http://www.dotnetcurry.com/ShowArticle.aspx?ID=563" rel="nofollow">http://www.dotnetcurry.com/ShowArticle.aspx?ID=563

我来自纯 C# 背景,但是,

例如,如果您有一个可观察集合的属性:

 public ObservableCollection<string> Collection {get; set;}

在 ViewModel 上,如果您通过设置视图的 DataContext 将视图(.xaml)绑定到 VM,并且已将此 OC 绑定到组合框,更改组合框上的 selectedValue 将更新您绑定的 SelectedValue到 .xaml 中。

因此,如果您选择将此组合框在 SelectedValue 上绑定到属性:

 public string SelectedValue {get; set;}

当您选择组合框中的选项之一时,将更新视图模型上的该值,并且它将进入属性的设置部分。

对于绑定到 GridView 的任何内容也是如此。如果您的 gridview 也绑定了 ObservableCollection,则您需要在视图上连接事件,以便您认为更改应该更新 ViewModel 并使用 ICommand 将更改传播到 VM。

另一个问题是你需要使用VB吗?我知道 Caliburn.Micro 等使 MVVM 更容易的框架是用 C# 编写的,肯定有一些框架可以使 MVVM 与 VB 一起变得更容易。尽管根据您正在尝试的内容,基本 MVVM 的设置并不难,但这些框架通常可以节省您手动连接绑定的时间。

IF you're using MVVM, then your bindings (if wired properly) will propagate changes you make in the GridView to the properties their bound too if you wire the events when a cell edit is finished, etc.

Here's a source on that:

http://www.dotnetcurry.com/ShowArticle.aspx?ID=563

I'm coming from a C# only background but,

for instance if you have a property of an observable collection:

 public ObservableCollection<string> Collection {get; set;}

on the ViewModel, if you had bound your view (the .xaml) to the VM by setting the DataContext of the View, and you had bound this OC to a combobox changing the selectedValue on the combobox would update the SelectedValue you bound to in the .xaml.

So if you had chosen for this combobox to be bound on SelectedValue to a property:

 public string SelectedValue {get; set;}

when you selected one of the choices in the combobox would update that value on the view model and it would step into the set portion of the property.

The same goes for whatever you have bound to the GridView. If you have an ObservableCollection that your gridview binds too, you'll need to wire up the events on the view for when you feel a change should update the ViewModel and use the ICommand to propagate the change to the VM.

Another question would be are you required to use VB? I know the frameworks that make MVVM easier like Caliburn.Micro are in C#, there must be some frameworks out there to make MVVM easy with VB. Although with what you're trying, basic MVVM wouldn't be that hard to setup, these frameworks usually save you time from having to wire up the bindings manually.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文