![](/img/trans.png)
[英]How to update dataGrid in WPF from another window without using MVVM?
[英]How can i add a record to a datagrid and update it from another window?
我在更新数据网格的内容时遇到问题。 我想从另一个 window 向数据网格中添加一行,用户在其中填写信息。 然后单击按钮时,数据网格应更新并显示添加的行。 但是现在,它没有,我无法弄清楚我做错了什么。
因此,当添加一个帐户时,它应该使用新添加的记录更新主 window 中的数据网格。
现在,当我想从同一个 window 中添加记录时,这就是我添加记录的方式(效果很好)
public ObservableCollection<Account> ListAccountInfo { get; } = new ObservableCollection<Account>();
public MainWindow()
{
InitializeComponent();
ListAccountInfo.Add(new Account
{
IsSelected = true,
qweqwe = "test123",
qweqwe = "test123",
qwe = "N/A",
qwe = "N/A",
qwe = "N/A",
qwe = "N/A",
qwe = "N/A",
qwe = "N/A"
});
}
但是当我尝试从另一个 window (带有表单的那个)后面的代码中做同样的事情时,它不会将新记录添加到列表中。
账户class只是一堆get; 放;。 不包含任何其他内容(如果需要,我可以添加它..)
我希望有人可以帮助我。 我对 wpf 还不是很有经验❤
最简单的解决方案是使用 windows 和ICommand
的共享DataContext
实例来实际将新的Account
项添加到集合中:
中继命令.cs
实施取自Microsoft Docs:模式 - WPF 具有模型-视图-视图模型设计模式的应用程序 - 中继命令逻辑
public class RelayCommand : ICommand
{
#region Fields
readonly Action<object> _execute;
readonly Predicate<object> _canExecute;
#endregion // Fields
#region Constructors
public RelayCommand(Action<object> execute) : this(execute, null) { }
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute; _canExecute = canExecute;
}
#endregion // Constructors
#region ICommand Members
[DebuggerStepThrough]
public bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute(parameter);
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameter) { _execute(parameter); }
#endregion // ICommand Members
}
视图模型.cs
public class ViewModel : INotifyPropertyChanged
{
public ViewModel()
{
this.NewAccount = new Account();
this.ListAccountInfo = new ObservableCollection<Account>()
{
new Account
{
IsSelected = true,
Username = "test123",
qweqwe = "test123",
qwe = "N/A",
qwe = "N/A",
qwe = "N/A",
qwe = "N/A",
qwe = "N/A",
qwe = "N/A"
}
};
}
public ObservableCollection<Account> ListAccountInfo { get; }
private Account newAccount;
public Account NewAccount
{
get => this.newAccount;
set
{
this.newAccount = value;
OnPropertyChanged();
}
}
public ICommand AddAccountCommand
{
get => new RelayCommand(
param =>
{
this.ListAccountInfo.Add(this.NewAccount);
this.NewAccount = new Account();
});
}
#region Implementation of INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion Implementation of INotifyPropertyChanged
}
App.xaml
<Application>
<Application.Resources>
<!-- Globally shared ViewModel instance -->
<ViewModel x:Key="ViewModel" />
</Application.Resources>
</Application>
主窗口.xaml
<Window>
<Window.DataContext>
<StaticResource ResourceKey="ViewModel" />
</Window.DatContext>
<DataGrid ItemsSource="{Binding ListAccountInfo}" />
</Window>
DialogWindow.xaml
<Window.DataContext>
<StaticResource ResourceKey="ViewModel" />
</Window.DatContext>
<StackPanel>
<TextBox Text="{Binding NewAccount.Username}" />
<Button Content="Add"
Command="{Binding AddAccountCommand}" />
</StackPanel>
</Window>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.