Updating SelectedItem does not update Collection RRS feed

  • Question

  • Hey All,

    My intention is to update the values datagrid with the help of TextBox control;

    Here is the scenario;

    DataGrid is binded to a PagedCollectionView, There is a textbox, wherein the Text is binded to DataGrid's SelectedItem's Property. (Mode is TwoWay) - I am trying to update the SelectedItem Property from the ViewModel, and it reflects the value in the binded textbox, but not updating the collection, meaning, the collection binded to DataGrid. What could be the reason?

    Here is the code

    private void OnCancel()
                Region selectedRegion = this.selectedRegion != null && this.allRegions.Where(o => o.RegionCode == this.selectedRegion.RegionCode).Count() > 0 ? this.allRegions.Where(o => o.RegionCode == this.selectedRegion.RegionCode).First() : null;                                
                    this.selectedRegion = selectedRegion;
                    RaisePropertyChanged("FilteredRegions"); // Tried using it since the collection was not getting updated

    This is actually a reset, since the user hits the cancel button

    Friday, February 11, 2011 6:20 AM

All replies

  • Hi,

    If I understand TextBox's default behavios update source when lost focus. But you can override that UpdateSourceTrigger=Explicit, Then handler that in code.

    Binding class in SL does not implement PropertyChanged.

    Friday, February 11, 2011 7:01 AM
  • Thank you for your quick response. As you can see, I am not tryting to update the value from the TextBox, so UpdateSourceTrigger does'nt have any connection with this;

    When I try to update the value explicitly, i.e. Try to change each and every property value, it works.

    e.g: this.selectedItem.RegionName = selectedItem.RegionName;

    (I have implemented the INotify in the Region class as well - for code, name etc)

    Again, I believe, this is not the right way as we need to update each and every property saperately

    Saturday, February 12, 2011 10:03 PM
  • A paged collection view implements ICollectionView, so the Pcv it self does have its own CurrentItem property. I suspect what is happening is your VM exposes a SelectedRegion property which your textbox is bound to, but your datagrid is probably bound to the collection's currentItem and not the VM's SelectedRegion.

    Can you paste the code your using for your datagrid?

    Monday, April 4, 2011 2:47 AM
  • Hi,

    You can use Prism assembly.

    Custom Behaviors

    UpdateTextBindingOnPropertyChanged. This custom behavior updates the source of a binding on a text box as the text changes. In this way, the input can be validated whenever a new character is entered or deleted.


    Monday, April 4, 2011 4:47 AM
  • Not sure, whether it is the right answer, since I dont wanted to use Prism libraries, I updated the properties by creating a new instance of the collection;

    Here is a sample for your reference

    foreach (var item in salariesFromServer)
                    Salaries.Add(new Salary
                        BasicPay = item.BasicPay,
                        HRA = item.HRA,
                        TA = item.TA,
                        OtherAllowances = item.OtherAllowances,
                        Remarks = item.Remarks

    Not sure, whether this is the right method. But I think I am now having a solution.

    Wednesday, April 20, 2011 11:07 PM
  • Hi neal.gab...,

    I use this to update UI.

        public class ViewModel : NotificationObject
            public ViewModel()
                if (!DesignerProperties.IsInDesignTool)
                    this.RefreshCommand = new DelegateCommand<object>(this.OnRefresh);
                    this.customers = new ObservableCollection<Customer>(_context.Customers);

            ForsoftFXDomainContext _context = new ForsoftFXDomainContext();
            public ObservableCollection<Customer> customers { get; set; }

            public ICommand RefreshCommand { get; private set; }

    //Fetch new data that has inserted from different PC then Update UI

            private void OnRefresh(object sender)
                EntityQuery<Customer> query = from db in _context.GetCustomersQuery() select db;
                LoadOperation<Customer> lop = this._context.Load(query, LoadBehavior.MergeIntoCurrent, false);
                lop.Completed += (s, e) =>
                        var result = lop.Entities.Except(this.customers);
                        foreach (var item in result)

    use in view's code behind

            public ViewModel MainViewModel
                set { this.DataContext = value; }

    Friday, April 22, 2011 9:51 AM