Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Try using an <a href="http://msdn.microsoft.com/en-us/library/ms748365.aspx" rel="nofollow"><code>Observable Collection</code></a> instead of a normal list. This collection implements INotifyCollectionChanged already.</p> <p>Note that this will work for adding or removing items in the list, but if you change the item's properties themselves and want to update the <code>ObservableCollection</code>, you need to have an <code>ObservableCollection</code> of <code>ViewModels</code>, which are implementing <code>INotifyPropertyChanged</code> on each property.</p> <hr> <h2>EDIT</h2> <p>This may be a silly question but where are you actually calling that x method? I copied your code pretty much exactly, created my own Result class, and implemented INotifyPropertyChanged and created an <a href="http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/d44932d3-ba6a-4ca1-88e0-c34665123a5c/" rel="nofollow">implementation of the RelayCommand pattern</a>, bound that to the command of the button and it all worked. When I click the button, the datagrid changes.</p> <p>All I can think of is that you haven't actually implemented the INotifyPropertyChanged, or you aren't running the x method.</p> <p>here is the code I did:</p> <pre><code> public class Model : INotifyPropertyChanged { ObservableCollection&lt;Result&gt; _results = new ObservableCollection&lt;Result&gt;(); private List&lt;string&gt; Files; public void X() { foreach (var file in Files) { _results.Add(new Result() { File = file, Status = "passsed" }); } _results.Add(new Result() { File = DateTime.Now.ToString(), Status = "passed" }); } public ObservableCollection&lt;Result&gt; Results { get { return _results; } set { _results = value; OnPropertyChanged("Results"); } } public ICommand XCmd { get; protected set; } private void InitializeCommands() { this.XCmd = new RelayCommand((param) =&gt; { this.X(); }, (param) =&gt; { return true; }); } public Model() { Files = new List&lt;string&gt;(); Files.Add("ONE"); Files.Add("TWO"); Files.Add("THREE"); Files.Add("FOUR"); _results.Add(new Result() { File = "ZERO", Status = "Pending" }); _results.Add(new Result() { File = DateTime.Now.ToString(), Status = "Pending" }); InitializeCommands(); } #region INotifyPropertyChanged Members /// &lt;summary&gt; /// Raised when a property on this object has a new value. /// &lt;/summary&gt; public event PropertyChangedEventHandler PropertyChanged; /// &lt;summary&gt; /// Raises this object's PropertyChanged event. /// &lt;/summary&gt; /// &lt;param name="propertyName"&gt;The property that has a new value.&lt;/param&gt; protected virtual void OnPropertyChanged(string propertyName) { this.VerifyPropertyName(propertyName); PropertyChangedEventHandler handler = this.PropertyChanged; if (handler != null) { var e = new PropertyChangedEventArgs(propertyName); handler(this, e); } } #endregion // INotifyPropertyChanged Members #region Debugging Aides /// &lt;summary&gt; /// Warns the developer if this object does not have /// a public property with the specified name. This /// method does not exist in a Release build. /// &lt;/summary&gt; [Conditional("DEBUG")] [DebuggerStepThrough] public void VerifyPropertyName(string propertyName) { // Verify that the property name matches a real, // public, instance property on this object. if (TypeDescriptor.GetProperties(this)[propertyName] == null) { string msg = "Invalid property name: " + propertyName; if (this.ThrowOnInvalidPropertyName) throw new Exception(msg); else Debug.Fail(msg); } } /// &lt;summary&gt; /// Returns whether an exception is thrown, or if a Debug.Fail() is used /// when an invalid property name is passed to the VerifyPropertyName method. /// The default value is false, but subclasses used by unit tests might /// override this property's getter to return true. /// &lt;/summary&gt; protected virtual bool ThrowOnInvalidPropertyName { get; private set; } #endregion // Debugging Aides </code></pre> <p><em>Note that the INotifyPropertyChanged Members region implements the PropertyChanged event, and the Debugging aids region just checks that the property specified in the OnPropertyChanged handler actually exists.</em></p> <hr> <p>Here is the xaml:</p> <pre><code> &lt;Grid&gt; &lt;Grid.RowDefinitions&gt; &lt;RowDefinition&gt;&lt;/RowDefinition&gt; &lt;RowDefinition&gt;&lt;/RowDefinition&gt; &lt;/Grid.RowDefinitions&gt; &lt;DataGrid HorizontalAlignment="Stretch" Name="resultDataGrid" VerticalAlignment="Stretch" ItemsSource="{Binding Path=Results, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" /&gt; &lt;Button Grid.Row="2" Command="{Binding XCmd}" Margin="5,5,5,5"&gt;click&lt;/Button&gt; &lt;/Grid&gt; </code></pre> <p><em>Its not pretty I know but you can style it as you please</em></p> <hr> <p>And here is the relaycommand implementation that I linked you earlier:</p> <pre><code> public class RelayCommand : ICommand { #region Private Accessor Fields /// &lt;summary&gt; /// A boolean function that contains the code to enable/disable the command and the associated UI elements. /// &lt;/summary&gt; private readonly Func&lt;object, bool&gt; _canExecute = null; /// &lt;summary&gt; /// A generic delegate that will contain the code to execute. /// &lt;/summary&gt; private readonly Action&lt;object&gt; _executeAction = null; #endregion //Private Accessor Fields #region Constructor /// &lt;summary&gt; /// Initializes a new instance of the RelayCommannd class /// &lt;/summary&gt; /// &lt;param name="executeAction"&gt;The execute action.&lt;/param&gt; /// &lt;param name="canExecute"&gt;The can execute.&lt;/param&gt; public RelayCommand(Action&lt;object&gt; executeAction, Func&lt;object, bool&gt; canExecute) { this._executeAction = executeAction; this._canExecute = canExecute; } #endregion //Modified on 15 August 2011. CanExecuteChanged #region Implementation of ICommand /// &lt;summary&gt; /// Occurs when changes occur that affect whether or not the command should execute. /// &lt;/summary&gt; public event EventHandler CanExecuteChanged { //RequerySuggested occurs when the CommandManager detects conditions that might //change the ability of a command to execute. add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } /// &lt;summary&gt; /// Defines the method that determines whether the command can execute in its current state. /// &lt;/summary&gt; /// &lt;param name="parameter"&gt;Data used by the command. If the command does not require data to be passed, /// this object can be null.&lt;/param&gt; /// &lt;returns&gt;true if this command can be executed; otherwise, false.&lt;/returns&gt; public bool CanExecute(object parameter) { if (this._canExecute == null) { return true; } return this._canExecute(parameter); } /// &lt;summary&gt; /// Defines the method to be called when the command is invoked. /// &lt;/summary&gt; /// &lt;param name="parameter"&gt;Data used by the command. If the command does not require data to be passed, /// this object can be set to null&lt;/param&gt; public void Execute(object parameter) { if (this._executeAction != null) { this._executeAction(parameter); } } #endregion </code></pre> <p>If this doesn't work you will have to show me more of your code, because I really don't know why it's not working.</p>
    singulars
    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.
    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