Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Implementing a ViewModel for the First time

$
0
0

First of all I'm not sure if I should have marked this as a question or discussion as I'm trying to figure out how to implement MVVM in a simple WPF application so I will naturally develop more questions as things are explained to me. This is 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 noticed that a lot of developers will 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?



-- Tyler Hughes


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>