问题:将userId文本框值添加到Datagrid但是当在datagrid中选择行时,它不会填充文本框。我想创建应用程序,因此文本框值填充到datagrid,datagrid行选择填充到同一应用程序的文本框中。但是我一次能够进行一次操作。
Xmal代码:
<Window x:Class="MvvMWithWPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:MvvMWithWPF"
mc:Ignorable="d"
Title="MainWindow" Height="485" Width="525"
d:DataContext="{d:DesignInstance Type=local:UserViewModel}">
<Grid >
<Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto" ></RowDefinition>
<RowDefinition Height="Auto" ></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<DataGrid Grid.Row="0" Height="300" Width="450" Grid.ColumnSpan="2" ItemsSource="{Binding Users}" SelectedItem="{Binding User}" Name="EmpDataGrid" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn x:Name="empId" Header="empIdcolumn" Width="85" Binding="{Binding UserId}"></DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
<TextBox Grid.Row="2" Grid.Column="1" Height="35" Width="200" HorizontalAlignment="Center" VerticalAlignment="Bottom" Text="{Binding User.UserId, Mode=TwoWay ,UpdateSourceTrigger=PropertyChanged}" />
<Button Content="Update" Grid.Row="2" Height="35" HorizontalAlignment="Right" Name="btnUpdate"
VerticalAlignment="Bottom" Width="85"
Command="{Binding Path= UpdateCommand}" Grid.Column="2" />
</Grid>
ViewModel代码:
public class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged(string name)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
public class UserViewModel : ViewModelBase
{
private ObservableCollection<User> _UsersList;
private User _user;
public UserViewModel()
{
_user = new User();
_UsersList = new ObservableCollection<User>();
}
public ObservableCollection<User> Users
{
get { return _UsersList; }
set
{
_UsersList = value;
OnPropertyChanged("Users");
}
}
public User User
{
get
{
return _user;
}
set
{
_user = value;
OnPropertyChanged("User");
}
}
private ICommand mUpdater;
public ICommand UpdateCommand
{
get
{
if (mUpdater == null)
mUpdater = new Updater(this);
return mUpdater;
}
//set
//{
// mUpdater = value;
//}
}
private void Submit()
{
Users.Add(User);
User = new User();
}
private class Updater : ICommand
{
#region ICommand Members
UserViewModel _obj;
public Updater(UserViewModel obj)
{
_obj = obj;
}
public bool CanExecute(object parameter)
{
return true;
}
public event EventHandler CanExecuteChanged;
public void Execute(object parameter)
{
_obj.Submit();
}
}
#endregion
}
型号代码:
public class User : ViewModelBase
{
private int userId;
public int UserId
{
get
{
return userId;
}
set
{
userId = value;
OnPropertyChanged("UserId");
}
}
}