Click here to Skip to main content
       

Silverlight / WPF

 
QuestionBinding issues with Treeview C# WPF Pinmemberbartbartb2-May-13 11:41 
AnswerRe: Binding issues with Treeview C# WPF PinprofessionalKenneth Haugland3-May-13 2:22 
AnswerRe: Binding issues with Treeview C# WPF PinprofessionalMycroft Holmes9-May-13 12:23 
QuestionVisualTreeHelper.HitTest / RectangleGeometry not working... PinmemberSledgeHammer012-May-13 7:26 
AnswerRe: VisualTreeHelper.HitTest / RectangleGeometry not working... PinprofessionalKenneth Haugland2-May-13 7:38 
QuestionTab Closing Problem PinmemberKevin Marois1-May-13 20:14 
AnswerRe: Tab Closing Problem PinmemberAmitosh Swain3-May-13 4:49 
QuestionAsynchronous Call Using MVVM Pinmemberjonkjon29-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 PinmemberSledgeHammer0129-Apr-13 10:32 
GeneralRe: Asynchronous Call Using MVVM Pinmemberjonkjon29-Apr-13 10:48 
Questionwin32 window as c# wpf control Pinmembermkusnjer29-Apr-13 8:58 
AnswerRe: win32 window as c# wpf control PinmemberSledgeHammer0129-Apr-13 9:28 
GeneralRe: win32 window as c# wpf control Pinmembermkusnjer30-Apr-13 5:19 
GeneralRe: win32 window as c# wpf control PinmemberSledgeHammer0130-Apr-13 10:58 
GeneralRe: win32 window as c# wpf control Pinmembermkusnjer1-May-13 8:31 
GeneralRe: win32 window as c# wpf control PinmemberSledgeHammer011-May-13 10:46 
GeneralRe: win32 window as c# wpf control Pinmembermkusnjer1-May-13 12:11 
GeneralRe: win32 window as c# wpf control PinmemberSledgeHammer011-May-13 12:20 
GeneralRe: win32 window as c# wpf control Pinmembermkusnjer1-May-13 12:39 
GeneralRe: win32 window as c# wpf control PinmemberSledgeHammer011-May-13 13:00 
GeneralRe: win32 window as c# wpf control Pinmembermkusnjer1-May-13 13:24 
AnswerRe: win32 window as c# wpf control PinmemberAmitosh Swain3-May-13 4:33 
QuestionDynamically create radiobuttons under the group header that can span to N - number of groups PinmemberRajesh Sajjanar27-Apr-13 10:29 
AnswerRe: Dynamically create radiobuttons under the group header that can span to N - number of groups PinprofessionalKenneth Haugland27-Apr-13 12:32 
AnswerRe: Dynamically create radiobuttons under the group header that can span to N - number of groups PinprofessionalMycroft 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
Web02 | 2.8.140415.2 | Last Updated 16 Apr 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid