簡體   English   中英

如何將記錄添加到數據網格並從另一個 window 更新它?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM