Hi all,
I need help understanding why I can't edit the data grid so that the changes are reflected in the bound data. I have the following class:
public class Player : INotifyPropertyChanged { public Player() { } public Player(string name) { _name = name; } private string _name = ""; public string Name { get { return _name; } set { _name = value; FirePropertyChanged("Name"); } } private ObservableCollection<int> _gameScoreList = new ObservableCollection<int>(); public ObservableCollection<int> GameScoreList { get { return _gameScoreList; } set { if (_gameScoreList != value) { _gameScoreList = value; FirePropertyChanged("GameScoreList"); } } } private string _currentScore = ""; public string CurrentScore { get { return this.GameScoreList.Sum().ToString(); } set { _currentScore = value; FirePropertyChanged("CurrentScore"); } } private string _lastWordScore; public string LastWordScore { get { return this.GameScoreList.Last<int>().ToString(); } set { _lastWordScore = value; FirePropertyChanged("LastWordScore"); } } private bool _turn = false; public bool Turn { get { return _turn; } set { _turn = value; FirePropertyChanged("Turn"); } } private string _currentWord = ""; public string CurrentWord { get { return _currentWord; } set { _currentWord = value; FirePropertyChanged("CurrentWord"); } } private List<Game> _gameHistory = new List<Game>(); public List<Game> GameHistory { get { return _gameHistory; } set { _gameHistory = value; } } private void FirePropertyChanged(string property) { if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(property)); } } public event PropertyChangedEventHandler PropertyChanged; }
I have the following DataGrid:
<DockPanel x:Name="dp1" Grid.Column="1" HorizontalAlignment="Stretch" Grid.RowSpan="2"><WrapPanel x:Name="spP2Name" DockPanel.Dock="Top" Margin="60, 0, 0, 0" Orientation="Horizontal" HorizontalAlignment="Center"></WrapPanel><TextBox x:Name="tbP2Curr" DockPanel.Dock="Top" MinWidth="150" HorizontalAlignment="Center" TextAlignment="Center" VerticalAlignment="Center" Text="{Binding Path=CurrentWord}" Style="{StaticResource TotalHeaderStyle}"></TextBox><DataGrid x:Name="dgP2Scores" DockPanel.Dock="Top" ItemsSource="{Binding Path=GameScoreList, Mode=TwoWay, NotifyOnTargetUpdated=True, UpdateSourceTrigger=LostFocus}" IsReadOnly="False"><DataGrid.Columns><DataGridTextColumn Binding="{Binding Path=., Mode=TwoWay}"/></DataGrid.Columns></DataGrid></DockPanel>
I've then set the DataContext of my DockPanel to a Player Class like so:
dp1.DataContext = p;
Now I can happily add new items to the datagrid, but if I try and manually overwrite one by editing a DataGrid cell then it doesn't change, nor does it try to change.
I've got to be doing something daft here but I just can't see what?
Thanks all,
Jib