Hi
I am working in wpf with mvvm light. I want to select data from data grid and put them in textboxes.
let me explain my project scenario.
UI:
- <Grid ShowGridLines="True">
- <Grid.RowDefinitions>
- <RowDefinition>RowDefinition>
- <RowDefinition>RowDefinition>
- Grid.RowDefinitions>
- <StackPanel Orientation="Vertical" HorizontalAlignment="Center"
- Grid.Row="0"
- VerticalAlignment="Center">
- <StackPanel Orientation="Horizontal" Margin="0,0,0,10">
- <TextBlock Name="name" Text="Name:" Margin="0,0,30,0">TextBlock>
- <TextBox x:Name="text1"
- Text="{Binding UserLoginData.Name,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
- Width="100">TextBox>
- StackPanel>
-
- <StackPanel Orientation="Horizontal" Margin="0,0,0,10">
- <TextBlock Name="password" Text="Password:" Margin="0,0,12,0">TextBlock>
- <TextBox x:Name="text2"
- Text="{Binding UserLoginData.Password,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"
- Width="100">TextBox>
- StackPanel>
-
- <StackPanel Orientation="Horizontal" Margin="0,0,0,0">
- <Button Content="Login" Margin="0,0,25,0" Padding="5"
- Command="{Binding SaveCommand}">
- Button>
- StackPanel>
- StackPanel>
-
- <DataGrid Grid.Row="1" x:Name="MainDataGrid"
- ItemsSource="{Binding users}"
- SelectedItem="{Binding Path=SelectedUsers,Mode=TwoWay}" SelectionChanged="MainDataGrid_SelectionChanged">
- DataGrid>
- Grid>
I have two text boxes here Name and password. When we values of them that values sotres in my database and add to Datagrid as below image.
Now what i want is when we select on one of the data in Datagrid it should be add into that two text boxes so i can perform update, delete from it. but here i couldn't get data from datagrid in text boxes when i select on grid.
I am providing you my viewmodel, datacontext and model class here. so you can find solution from it.
View model class:
- public class UserLoginViewModel:ViewModelBase
- {
-
- public UserLoginViewModel()
- {
- UserLoginData = new UserLoginData();
- users = new ObservableCollection(GetUsers());
- }
-
- internal IEnumerable GetUsers()
- {
- DataBaseContext baseContext = new DataBaseContext();
- return baseContext.userLogins.ToList();
- }
-
- private ObservableCollection _SelectedUsers;
- public ObservableCollection SelectedUsers
- {
- get => _SelectedUsers;
- set
- {
- if(value != _SelectedUsers)
- {
- _SelectedUsers = value;
- RaisePropertyChanged();
- }
- }
- }
-
- private ObservableCollection _users;
- public ObservableCollection users
- {
- get => _users;
- set { _users = value; RaisePropertyChanged(); }
- }
-
- private UserLoginData _UserLoginData;
- public UserLoginData UserLoginData
- {
- get => _UserLoginData;
- set { _UserLoginData = value; RaisePropertyChanged(); }
- }
-
- public ICommand SaveCommand => new RelayCommand(SaveData);
-
- public object ObservableCollectoin { get; private set; }
-
- private void SaveData()
- {
- DataBaseContext baseContext = new DataBaseContext();
- baseContext.userLogins.Add(UserLoginData);
- baseContext.SaveChanges();
- }
- }
- }
Datacontext:
- public class DataBaseContext: DbContext
- {
- public DataBaseContext() : base("DbBindingContext")
- {
- }
-
- public DbSet userLogins { get; set; }
- }
Model:
- public class UserLoginData
- {
- [Key]
- public int Id { get; set; }
- public string Name { get; set; }
- public string Password { get; set; }
- }
tell me where i am wrong. and what shoud i do.
hope will get better solution. Thanks in advance.