Click here to Skip to main content
15,888,224 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am adding a record from view 2 in the database;
1. The record is inserted successfully and is instantly shown in the usercontrol(DataGrid) on the same view(View2)
2. The changes are not shown in view 1 Unless I Close the the view or application and start it again.

// The INotifyProperty is implemented in ViewModelBase Class

View1
XML
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:UC="clr-namespace:HRSimplified.Controls"
x:Class="HRSimplified.MainWindow"
Title="MainWindow" Height="628" Width="986">
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="2*" />
        </Grid.ColumnDefinitions>

        <UC:EmployeeGridControl Grid.Column="1" />
    </Grid>
</Window>

View2
XML
<Window
    x:Class="HRSimplified.Windows.EmployeeDashboard"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:Converters="clr-namespace:HRSimplified.Converters"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="clr-namespace:HRSimplified.View"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:system="clr-namespace:System;assembly=mscorlib"
    xmlns:VM="clr-namespace:HRSimplified.ViewModel"
    xmlns:UC="clr-namespace:HRSimplified.Controls"
    mc:Ignorable="d"
    Title="EmployeeDashboard">
    <Window.DataContext>
        <VM:ViewModel_Employee />
    </Window.DataContext>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="2*" />
        </Grid.ColumnDefinitions>
                            <UC:EmployeeGridControl />
                            <ItemsControl ItemsSource="{Binding Path=emp}">
                                <StackPanel>                                   
                                    <TextBox EditValue="{Binding emp.Name, NotifyOnTargetUpdated=True, NotifyOnSourceUpdated=True}" Margin="5" />
<Button Command="{Binding AddCommand, UpdateSourceTrigger=PropertyChanged, NotifyOnTargetUpdated=True, NotifyOnSourceUpdated=True, ValidatesOnDataErrors=True}" Margin="5" Height="35" />
                                 </StackPanel>   
                            </ItemsControl>
</Window>

UserControl
XML
<UserControl 
x:Class="HRSimplified.Controls.EmployeeGridControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:Converters="clr-namespace:HRSimplified.Converters"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
xmlns:local="clr-namespace:HRSimplified.View"
xmlns:system="clr-namespace:System;assembly=mscorlib"
xmlns:VM="clr-namespace:HRSimplified.ViewModel"
mc:Ignorable="d" 
d:DesignHeight="450" 
d:DesignWidth="800">
<Grid>
<DataGrid x:Name="MasterData" MaxHeight="1080" ItemsSource="{Binding MasterData, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnTargetUpdated=True, NotifyOnSourceUpdated=True}">
            <DataGridTextColumn x:Name="Name" Binding="{Binding Mode=TwoWay}" />
                
        </DataGrid>
        
    </Grid>
</UserControl>


ViewModel_Employee

C#
public class ViewModel_Employee:ViewModelBase
    {
        public HRSimplifiedEntities Model = new HRSimplifiedEntities();
        private ObservableCollection<Model.Employee> _MasterData;
        public ObservableCollection<Model.Employee> MasterData
        {
            get
            {
                return _MasterData;
            }
            set
            {
                SetProperty(ref this._MasterData, value);
            }
        }
        private Employee _emp;

        
        private ICommand _submitCommand;
        public Employee emp
        {
            get { return _emp; }
            set
            {
                _emp = value;
                OnPropertyChanged("EmployeeCollection");
            }
        }

        public ViewModel_Employee()
        {
            MasterData = new ObservableCollection<Model.Employee> 
            (Model.Employees.ToList() as IEnumerable<Employee>);
        }
        public ICommand AddCommand
        {
            get
            {
                if (_submitCommand== null)
                {
                    _submitCommand = new RelayCommand(executeMethod, canExecuteMethod, false);
                }
                return _submitCommand;
            }
        }

        private bool canExecuteMethod(object parameter)
        {
            if (string.IsNullOrEmpty(emp.Name) || string.IsNullOrEmpty(emp.Gender) || 
                string.IsNullOrEmpty(emp.Salary.ToString()))
                return false;
            else
                return true;
        }
        private void executeMethod(object parameter)
        {
                MasterData.Add(emp);

                Model.Employees.Add(emp);
                Model.SaveChanges();

                System.Media.SystemSounds.Beep.Play();
        }


What I have tried:

I used the same view model on both views.
but I think it creates a new instance of the view model and datacontext on each view.
I want to resolve this issue.
Posted
Updated 29-Apr-19 0:04am

1 solution

Each view is getting its own collection.

View1 is updating only because it's reloading the entire collection (with updates from view2).

View1 needs a reference to view2's collection (that it can use in its grid).
 
Share this answer
 
Comments
Member 10528646 1-May-19 1:54am    
Hi Gerry Schmitz!
grid is a separate usercontrol added both in view1 and view2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900