Click here to Skip to main content
11,579,093 members (73,587 online)

Silverlight / WPF

 
QuestionBinding issues with Treeview C# WPF Pin
bartbartb2-May-13 11:41
memberbartbartb2-May-13 11:41 
AnswerRe: Binding issues with Treeview C# WPF Pin
Kenneth Haugland3-May-13 2:22
professionalKenneth Haugland3-May-13 2:22 
AnswerRe: Binding issues with Treeview C# WPF Pin
Mycroft Holmes9-May-13 12:23
professionalMycroft Holmes9-May-13 12:23 
QuestionVisualTreeHelper.HitTest / RectangleGeometry not working... Pin
SledgeHammer012-May-13 7:26
memberSledgeHammer012-May-13 7:26 
AnswerRe: VisualTreeHelper.HitTest / RectangleGeometry not working... Pin
Kenneth Haugland2-May-13 7:38
professionalKenneth Haugland2-May-13 7:38 
QuestionTab Closing Problem Pin
Kevin Marois1-May-13 20:14
memberKevin Marois1-May-13 20:14 
AnswerRe: Tab Closing Problem Pin
Amitosh Swain3-May-13 4:49
memberAmitosh Swain3-May-13 4:49 
QuestionAsynchronous Call Using MVVM Pin
jonkjon29-Apr-13 10:21
memberjonkjon29-Apr-13 10:21 
I am using this in the code behind for a xaml page and it works fine for retrieving the data for a listbox:

_______________________________________________________________

SLConnDomainContext dbContext = new SLConnDomainContext();
        private LoadOperation<Employee> loadOp;
        ObservableCollection<Employee> data = new ObservableCollection<Employee>();
 
        public Filters()
        {
            InitializeComponent();
            loadOp = this.dbContext.Load(this.dbContext.GetEmployeesQuery());
         loadOp.Completed += new EventHandler(loadOp_Completed);
 
        }
 
        void loadOp_Completed(object sender, EventArgs e)
        {
            foreach (Employee emp in loadOp.Entities)
            {
                data.Add(emp);
            }
 
            dataForm.ItemsSource = data;
 
        }

I would like to move to a MVVM approach and have created a viewmodel to retrieve the data and pass it into a view by creating an instance of my viewmodel and setting the datacontext property of my listbox. The viewmodel works fine when I am just creating some mock data and populating a list. However, due to the asynchronous nature of using a loadoperation with silverlight, the above code doesn't work with the MVVM approach for me. The data is retrieved but it appears that it is after my view has already loaded so it never appears. Below is my code:

Viewmodel:
______________________________________________________________

{
 
    public partial  class EmployeeService
    {
        private SLConnDomainContext Context = new SLConnDomainContext();
        private LoadOperation<Employee> loadOp;
        public ObservableCollection<Employee> employees = new ObservableCollection<Employee>();
        
        public EmployeeService()
        {
           
         
            loadOp = Context.Load(Context.GetEmployeesQuery(), LoadBehavior.MergeIntoCurrent, employeeCallBack, null);
             
        }
       
 

        void employeeCallBack(LoadOperation<Employee> op)
        {
            foreach (Employee emp in loadOp.Entities)
            {
                employees.Add(emp);
               
            }
            _myEmployees = employees;
	//using below to determine that data is retrieved
            MessageBox.Show("I have " + employees.Count + " employees");
        }
      
 
     
     
        public IEnumerable<Employee> GetEmployees()
       {
                  
            return employees;
        }
 
        public event PropertyChangedEventHandler PropertyChanged;
        private ObservableCollection<Employee> _myEmployees;
        public ObservableCollection<Employee> myEmployees
        {
            get { return _myEmployees; }
            set
            {
                myEmployees = value;
                OnPropertyChanged("myEmployees");
            }
        }
 

 
        private int _EmployeeID;
        public int EmployeeID
        {
            get { return _EmployeeID; }
            set
            {
                _EmployeeID = value;
                OnPropertyChanged("EmployeeID");
            }
        }
 
        private int _VacationHours;
        public int VacationHours
        {
            get { return _VacationHours; }
            set
            {
                _VacationHours = value;
                OnPropertyChanged("VacationHours");
            }
        }
 

        protected void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
       
    }
 
}

This is set in the constructor for the view's code behind:

______________________________________________________________

 public LayoutTest()
        {
            InitializeComponent();                  
           EmployeeService myservice = new EmployeeService();
           EmployeeList.DataContext = myservice;
                 
        }

I have the databinding set correclty as it works if I use something like this in place of the above GetEmployees():
______________________________________________________________


public IEnumerable<Employee> GetEmployees()
        {
            List<Employee> employees = new List<Employee>();
            EmployeeService test = new EmployeeService();
            foreach (Employee emp in test.GetEmployees())
            { 
            employees.Add(emp);
            }
              
            Employee employee = new Employee();
            employee.EmployeeID = 1111;
            employee.VacationHours = 100;
 
            employees.Add(employee);
 
            employee = new Employee();
            employee.EmployeeID = 1112;
            employee.VacationHours = 62;
            employees.Add(employee);
 
            employee = new Employee();
            employee.EmployeeID = 1113;
            employee.VacationHours = 132;
            employees.Add(employee);
            return employees;
        }

I am sure this is due to the asynchronous call but I don't know how to fix it. Thanks for any help.
AnswerRe: Asynchronous Call Using MVVM Pin
SledgeHammer0129-Apr-13 10:32
memberSledgeHammer0129-Apr-13 10:32 
GeneralRe: Asynchronous Call Using MVVM Pin
jonkjon29-Apr-13 10:48
memberjonkjon29-Apr-13 10:48 
Questionwin32 window as c# wpf control Pin
mkusnjer29-Apr-13 8:58
membermkusnjer29-Apr-13 8:58 
AnswerRe: win32 window as c# wpf control Pin
SledgeHammer0129-Apr-13 9:28
memberSledgeHammer0129-Apr-13 9:28 
GeneralRe: win32 window as c# wpf control Pin
mkusnjer30-Apr-13 5:19
membermkusnjer30-Apr-13 5:19 
GeneralRe: win32 window as c# wpf control Pin
SledgeHammer0130-Apr-13 10:58
memberSledgeHammer0130-Apr-13 10:58 
GeneralRe: win32 window as c# wpf control Pin
mkusnjer1-May-13 8:31
membermkusnjer1-May-13 8:31 
GeneralRe: win32 window as c# wpf control Pin
SledgeHammer011-May-13 10:46
memberSledgeHammer011-May-13 10:46 
GeneralRe: win32 window as c# wpf control Pin
mkusnjer1-May-13 12:11
membermkusnjer1-May-13 12:11 
GeneralRe: win32 window as c# wpf control Pin
SledgeHammer011-May-13 12:20
memberSledgeHammer011-May-13 12:20 
GeneralRe: win32 window as c# wpf control Pin
mkusnjer1-May-13 12:39
membermkusnjer1-May-13 12:39 
GeneralRe: win32 window as c# wpf control Pin
SledgeHammer011-May-13 13:00
memberSledgeHammer011-May-13 13:00 
GeneralRe: win32 window as c# wpf control Pin
mkusnjer1-May-13 13:24
membermkusnjer1-May-13 13:24 
AnswerRe: win32 window as c# wpf control Pin
Amitosh Swain3-May-13 4:33
memberAmitosh Swain3-May-13 4:33 
QuestionDynamically create radiobuttons under the group header that can span to N - number of groups Pin
Rajesh Sajjanar27-Apr-13 10:29
memberRajesh Sajjanar27-Apr-13 10:29 
AnswerRe: Dynamically create radiobuttons under the group header that can span to N - number of groups Pin
Kenneth Haugland27-Apr-13 12:32
professionalKenneth Haugland27-Apr-13 12:32 
AnswerRe: Dynamically create radiobuttons under the group header that can span to N - number of groups Pin
Mycroft Holmes27-Apr-13 13:06
professionalMycroft Holmes27-Apr-13 13:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Advertise | Privacy | Mobile
Web04 | 2.8.150603.1 | Last Updated 2 Jul 2015
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid