1

我有一个数据网格,它绑定到 Person 类型的可观察集合 所选项目绑定到 Object Person。我有 2 个文本框名字和姓氏。当用户从网格中选择一个项目时,文本框的值就会被填充。用户可以编辑值并单击提交按钮,值得到更新。

Source to Target 工作正常 - 即能够从 viewModel 显示当我更新值时得到更新。

假设用户选择了一个名字为 john, lastname smith 的项目问题是用户将名字编辑为 johnny 并且他没有单击提交按钮而是从数据网格中选择了不同的项目,所以当我回到原来的选定项目时。在网格中,所选项目显示为 John smith,但在文本框中,值显示为 Johnny smith。

4

2 回答 2

1

谢谢你们。使用 De-Activating 事件解决了该问题。因此,每当用户单击网格中的新项目时,旧项目 De_Activating 事件将检查数据是否已从原始数据更改,如果是,它将显示警告消息,让用户要么选择新项目,要么留下并完成编辑。如果用户希望留下并完成编辑,则使用 e.Cancel = true 取消事件;并且活动记录保留在旧项目中。如果用户继续进行新选择,则旧值将恢复到对象。

我相信可能会有更好的解决方案,我绝对愿意学习。非常感谢您的努力。对此,我真的非常感激。

于 2010-10-08T17:23:38.883 回答
0

首先,将 DataGrid 中的 SelectedItem 绑定到 ObservableCollection 中的项目。然后将 TextBox 控件绑定到 DataGrid 中的相同 SelectedItem(在我的情况下为 SelectedCustomer)。然后通过实现 INotifyPropertyChanged 来更新 SelectedCustomer,以使 ObservableCollection 与 SelectedCustomer 保持同步。最后,如果需要,您可以在 TextBox 控件中包含 UpdateSourceTrigger=PropertyChanged 以在输入 TextBox 时更新 DataGrid。

我已包含以下代码(ViewModelBase 除外)以帮助您入门。

这是带有一个 DataGrid 和两个 TextBox 控件的 XAML:

<Window x:Class="DataGridTextBox.Views.MainView"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:WpfToolkit="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit" 
  Title="Main Window" Height="400" Width="800">
  <DockPanel>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <WpfToolkit:DataGrid  
            Grid.Column="0"
            SelectedItem="{Binding Path=SelectedCustomer, Mode=TwoWay}"
            ItemsSource="{Binding Path=Customers, Mode=OneWay}" >
        </WpfToolkit:DataGrid>
        <StackPanel Grid.Column="1">
            <TextBlock Text="First Name"/>
            <TextBox Text="{Binding Path=SelectedCustomer.FirstName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
            <TextBlock Text="Last Name"/>
            <TextBox Text="{Binding Path=SelectedCustomer.LastName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
        </StackPanel>
    </Grid>
  </DockPanel>
</Window>

这是一个简单的 ViewModel:

public class MainViewModel : ViewModelBase
{

  public MainViewModel()
  {
     _customers = Customer.GetSampleCustomerList();
     _selectedCustomer = _customers[0];
  }

  private ObservableCollection<Customer> _customers = null;
  public ObservableCollection<Customer> Customers
  {
     get
     {
        return _customers;
     }
  }

  private Customer _selectedCustomer;
  public Customer SelectedCustomer
  {
     get
     {
        return _selectedCustomer;
     }
     set
     {
        _selectedCustomer = value;
        OnPropertyChanged("SelectedCustomer");
     }
  }
}

对于示例代码,我只是在此处将 View 的 DataContext 设置为 ViewModel:

public partial class App : Application
{
  private void OnStartup(object sender, StartupEventArgs e)
  {
     // Create the ViewModel and expose it using the View's DataContext
     Views.MainView view = new Views.MainView();
     view.DataContext = new ViewModels.MainViewModel();
     view.Show();
  }
}

最后是一个简单的客户定义:

public class Customer
{
  public String FirstName { get; set; }
  public String MiddleName { get; set; }
  public String LastName { get; set; }
  public String Address { get; set; }
  public Boolean IsNew { get; set; }

  // A null value for IsSubscribed can indicate 
  // "no preference" or "no response".
  public Boolean? IsSubscribed { get; set; }

  public Customer(String firstName, String lastName,
      String address, Boolean isNew, Boolean? isSubscribed)
  {
     this.FirstName = firstName;
     this.MiddleName = lastName;
     this.LastName = lastName;
     this.Address = address;
     this.IsNew = isNew;
     this.IsSubscribed = isSubscribed;
  }

  public static ObservableCollection<Customer> GetSampleCustomerList()
  {
     return new ObservableCollection<Customer>(new Customer[4] {
            new Customer("Jeff", "Zero", 
                "12 North Third Street, Apartment 45", 
                false, true), 
            new Customer("Joel", "One", 
                "34 West Fifth Street, Apartment 67", 
                false, false),
            new Customer("Jon", "Two", 
                "56 East Seventh Street, Apartment 89", 
                true, null),
            new Customer("Zamboni", "Three", 
                "78 South Ninth Street, Apartment 10", 
                true, true)
        });
  }
}
于 2010-10-06T22:06:49.137 回答