gpt4 book ai didi

wpf - 如何读取所有文本框值(映射到数据库字段)并将它们存储在 WPF 中的数据库中 - MVVM

转载 作者:行者123 更新时间:2023-12-03 10:14:20 25 4
gpt4 key购买 nike

我是 WPF 和 MVVM 的新手。经过长时间的发明,我知道如何从数据库中检索数据并将其绑定(bind)到 itemcontrol/listview/gridview。

但我的问题是我不知道如何读取一堆文本框值并将其作为新记录存储在数据库中。

这是我的示例代码..

看法

<ItemsControl ItemsSource="{Binding AllEmployees}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<TextBox Width="100" Text="{Binding FirstName}" Margin="4"/>
<TextBox Width="100" Text="{Binding LastName}" Margin="4"/>
<TextBox Width="100" Text="{Binding Age}" Margin="4"/>
</StackPanel>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
<!-- For new Employee Details -->
<StackPanel>
<TextBox x:Name="FirstName"/>
<TextBox x:Name="LastName"/>
<TextBox x:Name="Age"/>
<Button Content="New" Command="{Binding NewEmployeeCommand}"/>
</StackPanel>

我的cs文件是
    public ObservableCollection<DataAccess.Employee> AllEmployees
{
get;
private set;
}

public EmployeeListViewModel(EmployeeRepository employeeRepository)
{
if (employeeRepository == null)
{
throw new ArgumentNullException("employeeRepository");
}

_employeeRepository = employeeRepository;
this.AllEmployees = new ObservableCollection<DataAccess.Employee>
(_employeeRepository.ListAll());
}

现在我如何通过阅读这些文本框将新员工的名字、姓氏、年龄存储在数据库中。

如何为 编写函数新员工指挥读取文本框的事件( 文本框到数据库中相应数据文件的映射 )并将数据存储在数据库中。

多谢 !

最佳答案

如果您尝试使用 MVVM,只需:

  • 创建您的 ViewModel 以包含您的 View 需要的所有属性
  • 绑定(bind)到 Xaml 中的那些属性

  • 例如:
    public class EmployeeListViewModel
    {
    public ObservableCollection<Employee> AllEmployees {get;private set;}
    public string FirstName {get;set;}
    public string LastName {get;set;}
    public int? Age {get;set;}
    public ICommand NewEmployeeCommand {get;set;}

    //You need to connect to this method by using a Delegate/RelayCommand see link below
    public void AddNewEmployee()
    {
    //Add your real code here to actually insert into the db
    var result = InsertEmployeeIntoDatabase(FirstName,LastName,Age);
    //You probably want to add this new employee to the list now ;)
    AllEmployees.Add(result);
    //Now you probably want to reset your fields
    FirstName = null;
    LastName = null;
    Age = null;
    }
    }

    Click here for an implementation of a delegate command

    然后像这样编辑你的xaml:
    <ItemsControl ItemsSource="{Binding AllEmployees}">
    <ItemsControl.ItemTemplate>
    <DataTemplate>
    <StackPanel Orientation="Horizontal">
    <TextBox Width="100" Text="{Binding FirstName}" Margin="4"/>
    <TextBox Width="100" Text="{Binding LastName}" Margin="4"/>
    <TextBox Width="100" Text="{Binding Age}" Margin="4"/>
    </StackPanel>
    </DataTemplate>
    </ItemsControl.ItemTemplate>
    </ItemsControl>
    <!-- For new Employee Details -->
    <StackPanel>
    <TextBox Text={Binding FirstName}"/>
    <TextBox Text={Binding LastName}"/>
    <TextBox Text={Binding Age}"/>
    <Button Content="New" Command="{Binding NewEmployeeCommand}"/>
    </StackPanel>

    关于wpf - 如何读取所有文本框值(映射到数据库字段)并将它们存储在 WPF 中的数据库中 - MVVM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5789401/

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