gpt4 book ai didi

c# - 双向绑定(bind)到数据集 - 更新数据库?

转载 作者:太空宇宙 更新时间:2023-11-03 16:50:42 25 4
gpt4 key购买 nike

好吧,我是 WPF 和数据绑定(bind)的新手,但我搜索了又搜索,似乎找不到答案。

我有一个数据库(称为 Inventory)和一个数据集(称为 DevicesDataSet)。我想要做的是将数据集绑定(bind)到列表框,以便可以从 DevicesDataSet 的设备表中选择特定设备,并将其属性显示在文本框中以供编辑。

以下是我目前拥有的 XAML:

<Window x:Class="Inventory.SignOutDevice"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="SignOutDevice" Height="339" Width="392" xmlns:my="clr-namespace:Inventory" Loaded="Window_Loaded">
<Window.Resources>
<my:DevicesDataSet x:Key="devicesDataSet" />
<CollectionViewSource x:Key="devicesViewSource" Source="{Binding Path=Devices, Source={StaticResource devicesDataSet}}" />
</Window.Resources>
<Grid DataContext="{StaticResource devicesViewSource}">
<ListBox Height="237" HorizontalAlignment="Left" Margin="12,12,0,0" Name="listBox1"
VerticalAlignment="Top" Width="254" ItemsSource="{Binding}" SelectedValuePath="Selected">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Make}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<TextBox Margin="223,267,0,0" Text="{Binding Path=Make, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
</Grid>

Whenever a device is selected and a property is edited (I'm only displaying one property at the moment), the listbox is updated, but the dataset (database?) doesn't seem to be.也就是说,当我离开表单然后返回时,列表框会返回到其原始状态。

所以我想问题是:如何使这些更改持久化/写入数据库?

编辑:Derp,这是更新后的后端 C#:

using System.Windows;
using System.Data;
namespace Inventory
{
public partial class SignOutDevice : Window
{
DevicesDataSet devicesDataSet = null;
Inventory.DevicesDataSetTableAdapters.DevicesTableAdapter devicesDataSetDevicesTableAdapter = null;
public SignOutDevice()
{
InitializeComponent();
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
devicesDataSet = ((Inventory.DevicesDataSet)(this.FindResource("devicesDataSet")));
devicesDataSetDevicesTableAdapter = new Inventory.DevicesDataSetTableAdapters.DevicesTableAdapter();
devicesDataSetDevicesTableAdapter.Fill(devicesDataSet.Devices);
System.Windows.Data.CollectionViewSource devicesViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("devicesViewSource")));
devicesViewSource.View.MoveCurrentToFirst();
}

private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
devicesDataSet.AcceptChanges();
devicesDataSetDevicesTableAdapter.Update(devicesDataSet.Tables["Devices"].Select(null, null, DataViewRowState.Deleted));
devicesDataSetDevicesTableAdapter.Update(devicesDataSet.Tables["Devices"].Select(null, null, DataViewRowState.ModifiedCurrent));
devicesDataSetDevicesTableAdapter.Update(devicesDataSet.Tables["Devices"].Select(null, null, DataViewRowState.Added));
}

}
}

最佳答案

您是否在“输出”窗口中查看过绑定(bind)错误?因为在我看来你有一个。 TextBox 绑定(bind)到 Make 属性,但它的 DataContextdevicesViewSource 资源。没有任何内容将它与 ListBox 中的选定项相关联。

将两者关联起来的一种方法是为 ListBox 分配一个名称,然后将 TextBox 上的绑定(bind)设置为 {Binding ElementName=MyListBox, Path=Make, Mode=TwoWay}.

关于c# - 双向绑定(bind)到数据集 - 更新数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4053107/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com