Hi guys,
This is actually my first question here. I tend to find all the answers browsing the net, but haven't found the answer to that question yet...
*I've created this code especially for the question, look below please!*
I've been trying to figure out if there is any way to do something as follows:
1) I have a view model base with IPropertyChanged
2) I have a model for example a student model with 2 properties - ID and Name.
3) I have a simple MainWindow view, which is set on MainWindowViewModel datacontext.
4) The view shows a datagrid bounded to "Students" from MainWindowViewModel which is an OC of students - ObservableCollection<student>(with random data I inserted).
5) I've configured the datagrid to have SelectedItem = {Binding SelectedStudent}
6) I have set 2 text boxes to the SelectedStudent.ID and SelectedStudent.Name, and the values show up nicely whenever I select a student.
7) I also see the changes in the textbox's text in the UI, but not in code(!!!)
* I've set UpdateSourceTrigger=OnPropertyChanged, and binding Mode=TwoWay
The problem starts whenever I want to try to EDIT the properties values by changing the current value shown in the text boxes.
Maybe I don't understand what I'm doing while I bind this way, but I put a debug breakpoint on the "get/set" of my properties - ID and Name, and nothing seems to invoke the "Set" method(On the SelectedStudent - maybe that's part of my problem?).
This works if i work hard and create 2 new properties like "Id", and "Name" under the view model and bind them to the textboxes instead... I could change their value and the "set" would invoke i could see it on the breakpoint in the properties themselves - the problem starts only when I try to change my OBJECT's child properties...
Do I need to set the stackpanel "local" datacontext(for example) to something else???
Just to let you know, I'm working with PRISM, but I want to make this as simple as possible(if you know the answer in PRISM I would love to know!).
Thanks a lot to everyone who tries to help!
-----------------
CODE(Everything is here!):
-----
ViewModelBase:
public class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged([CallerMemberName]string caller = null)
{
var handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(caller));
}
}
}
----------------
MainWindowViewModel:
public class MainWindowViewModel : ViewModelBase
{
private ObservableCollection<Student> _students;
private Student _selectedStudent = null;
public Student SelectedStudent
{
get { return _selectedStudent; }
set
{
_selectedStudent = value;
OnPropertyChanged();
}
}
public ObservableCollection<Student> Students
{
get { return _students; }
set
{
_students = value;
OnPropertyChanged();
}
}
public MainWindowViewModel()
{
Students = new ObservableCollection<Student>();
Students.Add(new Student { Id = "0", Name = "Jackson" });
Students.Add(new Student { Id = "1", Name = "Jenny" });
}
}
-----------------
Student:
public class Student : ViewModelBase
{
private string _id;
private string _name;
public string Id
{
get { return _id; }
set { _id = value; OnPropertyChanged(); }
}
public string Name
{
get { return _name; }
set { _name = value; OnPropertyChanged(); }
}
}
-------------------
MainWindow.xaml:
<Window.DataContext>
<local:MainWindowViewModel/>
</Window.DataContext>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0">
<TextBlock Text="Name: "/>
<TextBox Text="{Binding SelectedStudent.Name, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
<TextBlock Text="Id: "/>
<TextBox Text="{Binding SelectedStudent.Id, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"/>
</StackPanel>
<DataGrid Grid.Row="1" x:Name="dgStudents" ItemsSource="{Binding Students, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" SelectedItem="{Binding SelectedStudent, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" AutoGenerateColumns="True"/>
</Grid>
What I have tried:
Everything I possibly could, many things I saw on the net... Tried going over binding guides but all they showed are mostly things I know.. I haven't found this specific editing case that I'm trying to do - pretty surprising...