Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
320
How to track Xamdatagrid datasource collcetion changed in MVVM
posted

Xamdatagrid is bind to EmployeeColl but when collection changes from combobox datasource property change is not firing.

How to get the datasource collection change on view model. So that I can do validation.

 Below is my viewmodel

ViewModel.cs

public class ViewModel : INotifyPropertyChanged
{
private ObservableCollection<string> _coll1;

public ICommand ClickCommand { get; set; }

public ObservableCollection<string> Coll1
{
get
{
return _coll1;
}
set
{
_coll1 = value;
NotifyPropertyChanged("Coll1");
}
}

private ObservableCollection<string> _coll2;

public ObservableCollection<string> Coll2
{
get
{
return _coll2;
}
set
{
_coll2 = value;
NotifyPropertyChanged("Coll2");
}
}

private ObservableCollection<Employee> _employeeColl;

public ObservableCollection<Employee> EmployeeColl
{
get
{
return _employeeColl;
}
set
{
_employeeColl = value;
NotifyPropertyChanged("EmployeeColl");
}
}

public ViewModel()
{
ClickCommand = new DelegateCommand(ClickExecute, ClickCanExecute);

Coll1 = new ObservableCollection<string> () { "AAA", "AASSSA", "AAAAXX"};
Coll2 = new ObservableCollection<string> () { "XX", "DD", "DDD" };

EmployeeColl = new ObservableCollection<Employee>() { new Employee("Emp1", "Dep1"), new Employee("Emp2", "Dep2") };


}

public void ClickExecute(object sender)
{

}

public bool ClickCanExecute(object sender)
{
return true;
}


public event PropertyChangedEventHandler PropertyChanged;

private void NotifyPropertyChanged(string propertyName = "")
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}

Below is my xaml

<StackPanel>
<igDP:XamDataGrid DataSource="{Binding Path=EmployeeColl, UpdateSourceTrigger=PropertyChanged}">
<igDP:XamDataGrid.FieldSettings>
<igDP:FieldSettings/>
</igDP:XamDataGrid.FieldSettings>

<igDP:XamDataGrid.FieldLayoutSettings>
<igDP:FieldLayoutSettings AddNewRecordLocation="OnTopFixed" AllowAddNew="True" AllowDelete="True"
SupportDataErrorInfo="RecordsAndCells" DataErrorDisplayMode="ErrorIconAndHighlight"/>
</igDP:XamDataGrid.FieldLayoutSettings>

<igDP:XamDataGrid.FieldLayouts>
<igDP:FieldLayout>
<igDP:ComboBoxField Name="Name" Width="*">
<igDP:ComboBoxField.EditorStyle>
<Style TargetType="igEditors:XamComboEditor">
<Setter Property="ItemsSource"
Value="{Binding Path=DataContext.Coll1,NotifyOnSourceUpdated=True, RelativeSource={RelativeSource AncestorType={x:Type igDP:XamDataGrid}}}" />
</Style>
</igDP:ComboBoxField.EditorStyle>
</igDP:ComboBoxField>
<igDP:ComboBoxField Name="Department" Width="*">
<igDP:ComboBoxField.EditorStyle>
<Style TargetType="igEditors:XamComboEditor">
<Setter Property="ItemsSource"
Value="{Binding Path=DataContext.Coll2, RelativeSource={RelativeSource AncestorType={x:Type igDP:XamDataGrid}}}" />
</Style>
</igDP:ComboBoxField.EditorStyle>
</igDP:ComboBoxField>
</igDP:FieldLayout>
</igDP:XamDataGrid.FieldLayouts>
</igDP:XamDataGrid>
<Button Height="20" Command="{Binding ClickCommand}"/>
</StackPanel>

  • 2490
    Offline posted

    Hello Yugandhar,

    Thank you for the code you have provided.

    If you want to change an item in the ObservableCollection (e.g. add, remove, move) you can use the CollectionChanged Event. I have modified the code you provided in to a small sample and added this event. Every time item is changed PropertyItemChanged will be called, for reference see in the sample.

    If this is not what you are trying to do please provide some more details:
    - Where you perform the interaction (e.g. the combobox)
    - What is the interaction (e.g. the current value in the combobox is .., I am changing it to..)
    - What is the expected result.

    Also if this sample is not an accurate representation of what you are trying to achieve please modify it and upload it. Having this information will help me to further investigate this for you.

    XamDataGrid_DataSource.zip