I make a demo on my understanding for your question, please check if I misunderstand. If I misunderstand, could you please give an operation result diagram to anlayze?
The ViewModel is:
class PersonViewModel:NotifyObject
{
private PersonModel userEdit;
public PersonModel UserEdit
{
get { return userEdit; }
set
{
userEdit = value;
OnPropertyChange("UserEdit");
}
}
private ObservableCollection<PersonModel> ltPersons = new ObservableCollection<PersonModel>();
public ObservableCollection<PersonModel> LtPersons
{
get { return ltPersons; }
set
{
this.ltPersons = value;
OnPropertyChange("LtPersons");
}
}
public PersonViewModel()
{
LtPersons = new ObservableCollection<PersonModel>() {
new PersonModel { Name = "John1", Age = 11, Pass=false,Email= new Uri("mailto:JOE+@school.com") },
new PersonModel { Name = "John2", Age = 12 , Pass=false,Email= new Uri("mailto:JOE+@school.com")},
new PersonModel { Name = "John3", Age = 13 , Pass=false,Email= new Uri("mailto:JOE+@school.com")} };
}
public ICommand ButtonCommand
{
get
{
return new DelegateCommand<PersonModel>((model) =>
{
UpdateUser updateUser = new UpdateUser();
updateUser.DataContext = model;
updateUser.Show();
});
}
}
}
The MainWindow is:
<Window.DataContext>
<local:PersonViewModel></local:PersonViewModel>
</Window.DataContext>
<Grid Width="800" Height="250" HorizontalAlignment="Left" >
<DataGrid Width="200" Height="200"
x:Name="dataGrid"
HorizontalAlignment="Left"
ItemsSource="{Binding LtPersons}"
AutoGenerateColumns="False"
SelectionMode="Extended"
SelectionUnit="FullRow"
SelectedItem="{Binding UserEdit}"
>
<DataGrid.Columns>
<DataGridTextColumn Header="Name" Width="80" Binding="{Binding Name,Mode=TwoWay}"/>
<DataGridTextColumn Header="Age" Width="50" Binding="{Binding Age,Mode=TwoWay}" />
</DataGrid.Columns>
</DataGrid>
<Button Height="18" Width="50" Content="Query" Command="{Binding ButtonCommand}" CommandParameter="{Binding UserEdit}" />
</Grid>
The UpdateWindow is:
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="300"/>
</Grid.RowDefinitions>
<Label Grid.Row="0" Grid.Column="0" Content="Name" HorizontalAlignment="Right" ></Label>
<TextBox Grid.Row="0" Grid.Column="1" Margin="4" Text="{Binding Name,Mode=TwoWay}" />
<Label Grid.Row="1" Grid.Column="0" Content="Age:" HorizontalAlignment="Right"/>
<TextBox Grid.Row="1" Grid.Column="1" Margin="4" Text="{Binding Age,Mode=TwoWay}"/>
<Label Grid.Row="2" Grid.Column="0" Content="Mail:" HorizontalAlignment="Right"/>
<TextBox Grid.Row="2" Grid.Column="1" Margin="4" Text="{Binding Email,Mode=TwoWay}"/>
<Label Grid.Row="3" Grid.Column="0" Content="Pass:" HorizontalAlignment="Right"/>
<TextBox Grid.Row="3" Grid.Column="1" Margin="4" Text="{Binding Pass,Mode=TwoWay}" />
<Button Grid.Row="4" Grid.Column="0" Content="Save" Click="Button_Click" ></Button>
</Grid>
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.