2 Replies - 8346 Views - Last Post: 28 February 2013 - 02:41 PM Rate Topic: -----

#1 RandomlyKnighted  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 115
  • View blog
  • Posts: 1,355
  • Joined: 14-January 10

Implementing a ViewModel for the First time in WPF

Posted 21 February 2013 - 03:39 PM

Hello,

I'm creating my first MVVM application with WPF. I have a model (shown below) and viewmodel already started. I'm just trying to figure out how to do the rest.

Address Model

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;

namespace QMAC.Models
{
    public class Address
    {
        private string _ethernetMAC;
        private string _wirelessMAC;

        /*
         *  Using the get accessor below we will be able to read the
         *  MAC Address and assign it to the data binding of the
         *  TextBlock. Since we are not using a set accessor this will
         *  make the property Read-Only. Also since this property is read only,
         *  we have to assign the MAC address to the fields in this class.
         */

        public Address()
        {
            _ethernetMAC = getEthernetMAC();
            _wirelessMAC = getWirelessMAC();
        }

        public string IPAddress
        {
            get { return _ipAddress; }
        }

        public string EthernetMAC
        {
            get { return _ethernetMAC; }
        }

        public string WirelessMAC
        {
            get { return _wirelessMAC; }
        }

        private string getEthernetMAC()
        {
            String mac = "";

            foreach (NetworkInterface address in NetworkInterface.GetAllNetworkInterfaces())
            {
                if (address.NetworkInterfaceType.Equals(NetworkInterfaceType.Ethernet) && address.OperationalStatus.Equals(OperationalStatus.Up))
                {
                    mac = address.GetPhysicalAddress().ToString();
                }
            }
            
            return mac;
        }

        private string getWirelessMAC()
        {
            String mac = "";
            foreach (NetworkInterface address in NetworkInterface.GetAllNetworkInterfaces())
            {
                if (address.NetworkInterfaceType.Equals(NetworkInterfaceType.Wireless80211) && address.OperationalStatus.Equals(OperationalStatus.Up))
                {
                    mac = address.GetPhysicalAddress().ToString();
                }
            }

            return mac;
        }
    }
}



Location Model

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace QMAC.Models
{
    public class Location
    {
        List<string> _sites;

        public Location()
        {
            _sites = getSites();
        }

        public List<string> site
        {
            get { return _sites; }
        }

        private List<String> getSites()
        {
            List<string> site_list = new List<string>();
            site_list.Add("Collinsville High School");
            site_list.Add("Crossville Elementary");
            site_list.Add("Crossville High School");
            site_list.Add("Fyffe High School");
            site_list.Add("Geraldine High School");
            site_list.Add("Henagar School");
            site_list.Add("Ider High School");
            site_list.Add("Plainview High School");
            site_list.Add("Ruhama School");
            site_list.Add("South DeKalb Primary");
            site_list.Add("Sylvania High School");
            site_list.Add("Valley Head High School");
            //schoolComboBox.ItemsSource = schools;
            return site_list;
        }
    }
}




ViewModelBase

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace QMAC.ViewModels
{
    class ViewModelBase
    {
        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void onpropertychanged(string propertyName)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
            {
                var e = new PropertyChangedEventArgs(propertyName);
                handler(this, e);
            }
        }
    }
}



As you can see from what I have so far I have a general ViewModelBase. I've been reading some of the tutorials and I have come up with a few questions. I've noticed that a lot of developers would have a ViewModelBase similar to mine above. Once they had that they would have another class that inherits the ViewModelBase. This class that inherits the ViewModelBase, is it the Model or is it a ViewModel?

Is This A Good Question/Topic? 0
  • +

Replies To: Implementing a ViewModel for the First time in WPF

#2 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1043
  • View blog
  • Posts: 4,057
  • Joined: 02-July 08

Re: Implementing a ViewModel for the First time in WPF

Posted 28 February 2013 - 02:33 PM

ViewModel.
Was This Post Helpful? 0
  • +
  • -

#3 RandomlyKnighted  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 115
  • View blog
  • Posts: 1,355
  • Joined: 14-January 10

Re: Implementing a ViewModel for the First time in WPF

Posted 28 February 2013 - 02:41 PM

I had already figured it out, just forgot to reply. For anyone else who is looking for the answer to this as well here you go.

Your ViewModelBase should have a propertyChanged method. It should also inherit iNotifyPropertyChanged. Any ViewModel you create should inherit your ViewModelBase class that way you can raise the propertyChanged method that you have created. This will let you update notify your View through databinding that the data has changed and it will update with the new information.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1