要開始,請將DataGrid中的SelectedItem綁定到ObservableCollection中的項目。然後將TextBox控件綁定到DataGrid中的SelectedItem(在我的例子中爲SelectedCustomer)。然後通過實現INotifyPropertyChanged來更新SelectedCustomer以保持SelectedCustomer的ObservableCollection insync。 最後,您可以在TextBox控件中包含UpdateSourceTrigger = PropertyChanged,以便在需要時在TextBox中鍵入的同時更新DataGrid。
我已經包含下面的代碼(ViewModelBase除外),讓你開始。
這裏是一個DataGrid和兩個文本框控件的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>
下面是一個簡單的視圖模型:
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");
}
}
}
有關示例代碼,我剛纔設置的DataContext的查看到視圖模型這裏:
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)
});
}
}