Note that there are some explanatory texts on larger screens.

plurals
  1. POWpf datagrid changing state from readonly to editable at runtime
    primarykey
    data
    text
    <p>I am new to WPF development yet. I just quickly created a sample to recreate the scenario. I am switching the IsReadonly Property of the datagrid in rowdetailsview based on the checkbox in the parent row.</p> <p>Everything works fine except in one particular scenario.</p> <p>How to recreate problem.</p> <p>Keep the initially create parent row checked. uncheck the parent row. go to the child row id property. clean everything in id field and tab out of that cell and you will see null reference exception.</p> <p>I have no clue how to fix this issue. Any insight will be really useful. </p> <p>code behind code:</p> <pre><code> namespace WpfApplication7 {/// &lt;summary&gt; /// Interaction logic for MainWindow.xaml /// &lt;/summary&gt; public partial class MainWindow : Window { public PerColl People { get; private set; } public MainWindow() { InitializeComponent(); this.People = new PerColl(); this.DataContext = this; } private void Button_Click(object sender, RoutedEventArgs e) { } public class Person : INotifyPropertyChanged, IEditableObject { public string Name { get; set; } public double Salary { get { return _salary; } set { if (this._salary == value) return; this._salary = value; if (this.PropertyChanged != null) this.PropertyChanged(this, new PropertyChangedEventArgs("Salary")); } } public bool IsLocked { get { return _isLocked; } set { if (this._isLocked == value) return; this._isLocked = value; if (this.PropertyChanged != null) this.PropertyChanged(this, new PropertyChangedEventArgs("IsLocked")); } } public ObservableCollection&lt;Kid&gt; Kids { get; set; } public Person(string name, double salary) { this.Name = name; this.Salary = salary; } public Person() { this.Salary = 10000; this.Name = "abc"; this.IsLocked = true; this.Kids = new ObservableCollection&lt;Kid&gt;(); this.Kids.Add(new Kid(1)); this.Kids.Add(new Kid(2)); } private bool _isLocked; private double _salary; #region INotifyPropertyChanged Members public event PropertyChangedEventHandler PropertyChanged; #endregion public void BeginEdit() { if (isEdit) return; isEdit = true; this.backup = this.MemberwiseClone() as Person; } public void CancelEdit() { if (!this.isEdit) return; isEdit = false; this.Name = this.backup.Name; this.Salary = this.backup.Salary; } public void EndEdit() { if (this.isEdit == false) return; this.isEdit = false; this.backup = null; } private bool isEdit; private Person backup; } public class PerColl : ObservableCollection&lt;Person&gt; { } public class Kid : IEditableObject,INotifyPropertyChanged { public int Id { get { return _id; } set { this._id = value; if (PropertyChanged != null) this.PropertyChanged(this, new PropertyChangedEventArgs("Id")); } } public string Name { get { return _name; } set { this._name = value; if (PropertyChanged != null) this.PropertyChanged(this, new PropertyChangedEventArgs("Name")); } } public Person Parent { get; set; } public Kid() { this.Id = 12345; this.Name = "kidname"; } public Kid(int id, string name = "kidname") { this.Id = 12345; this.Name = name; } #region IEditableObject Members public void BeginEdit() { if (isEdit) return; isEdit = true; this.backup = this.MemberwiseClone() as Kid; } public void CancelEdit() { if (!this.isEdit) return; isEdit = false; this.Id = backup.Id; } public void EndEdit() { if (this.isEdit == false) return; this.isEdit = false; this.backup = null; } #endregion private int _id; private string _name; private bool isEdit; private Kid backup; #region INotifyPropertyChanged Members public event PropertyChangedEventHandler PropertyChanged; #endregion } } } </code></pre> <p>Xaml Code:</p> <pre><code>&lt;Window x:Class="WpfApplication7.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"&gt; &lt;DockPanel LastChildFill="True" Margin="-1,5,1,-5"&gt; &lt;Button Content="Add" Height="30" Click="Button_Click" DockPanel.Dock="Top"/&gt; &lt;DataGrid ItemsSource="{Binding Path=People}" AutoGenerateColumns="False" SelectionUnit="CellOrRowHeader" RowDetailsVisibilityMode="Visible"&gt; &lt;DataGrid.Columns&gt; &lt;DataGridTextColumn Header="Salary" Binding="{Binding Path=Salary,StringFormat='{}{0:#,0}'}"/&gt; &lt;DataGridTextColumn Header="Name" Binding="{Binding Path=Name}"/&gt; &lt;DataGridCheckBoxColumn Header="IsLocked" Binding="{Binding Path=IsLocked,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/&gt; &lt;/DataGrid.Columns&gt; &lt;DataGrid.RowDetailsTemplate&gt; &lt;DataTemplate&gt; &lt;DataGrid ItemsSource="{Binding Path=Kids}" AutoGenerateColumns="False" IsReadOnly="{Binding Path=IsLocked}" SelectionUnit="CellOrRowHeader"&gt; &lt;DataGrid.Columns&gt; &lt;DataGridTextColumn Header="Id" Binding="{Binding Path=Id,StringFormat='{}{0:#,0}'}"/&gt; &lt;DataGridTextColumn Header="Name" Binding="{Binding Path=Name}"/&gt; &lt;/DataGrid.Columns&gt; &lt;/DataGrid&gt; &lt;/DataTemplate&gt; &lt;/DataGrid.RowDetailsTemplate&gt; &lt;/DataGrid&gt; &lt;/DockPanel&gt; </code></pre> <p></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload