gpt4 book ai didi

wpf - 绑定(bind)保存命令 WPF

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

我有一个在网格中有 3 个文本框的窗口 - 这是我的 View - 我有保存按钮,用于将新用户添加到我的用户列表中,其中包含来自文本框的数据。
我想在我的 View 模型类上使用中继命令来执行此操作,但我对如何进行绑定(bind)感到很困惑。我希望它足够清楚。任何想法或示例都会有所帮助。
提前致谢。

最佳答案

您应该有一个类似于以下内容的 ViewModel:

class UserViewModel
{
public String Name { get; set; }
public String Password { get; set; }
public String Email { get; set; }

public RelayCommand AddUserCommand { get; set; }

public UserViewModel()
{
AddUserCommand = new RelayCommand(AddUser);
}

void AddUser(object parameter)
{
// Code to add user here.
}
}

你可以像下面这样使用它:
    <StackPanel>
<TextBox Text="{Binding Name}"></TextBox>
<TextBox Text="{Binding Password}"></TextBox>
<TextBox Text="{Binding Email}"></TextBox>
<Button Command="{Binding AddUserCommand}">Add</Button>
</StackPanel>

要完成这项工作,请将以下代码放入您的 UserControl/Control/Window 的构造函数中:
DataContext = new UserViewModel();

关于wpf - 绑定(bind)保存命令 WPF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3403852/

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