ninja star A Neat Way to Set the Cursor in WPF

by Michael Ceranski, posted on November 30 2012

I found this excellent post on stack overflow which uses a Stack to set and unset the cursor. Normally when you want to set the wait cursor in your application you would use a try/finally block to ensure that the cursor eventually gets set back to the original value:

Mouse.OverrideCursor = Cursors.Wait;
try {
    return Foo.Execute();
}
finally
{
    Mouse.OverrideCursor = null;
}

Don't get me wrong here...There is nothing wrong with using a try/finally. However, there is an alternative way to solve the same problem which I personally think is a more elegant and foolproof. So without further ado, here is the OverrideCursor class.

static Stack<Cursor> s_Stack = new Stack<Cursor>();

public OverrideCursor(Cursor changeToCursor) {
    s_Stack.Push(changeToCursor);

    if (Mouse.OverrideCursor != changeToCursor)
        Mouse.OverrideCursor = changeToCursor;
}

public void Dispose() {
    s_Stack.Pop();

    Cursor cursor = s_Stack.Count > 0 ? s_Stack.Peek() : null;

    if (cursor != Mouse.OverrideCursor)
        Mouse.OverrideCursor = cursor;
}

With the help of this class, we can ditch the try/finally and use this block of code instead:

using (new OverrideCursor(Cursors.Wait)) {
    return Foo.Execute();
}

So what is happening here? Well, in the OverrideCursor's constructor the current cursor is pushed on the stack and the cursor is updated to whatever you value you passed in as an argument. Later on when the object is Disposed the original cursor is fetched from the stack and restored. Since the code that launches my dialog is wrapped in a using statement the OverrideCursor instance will be disposed as soon as my form is displayed. Neat Trick!

Tags:

ninja star My First Arduino Robot

by Michael Ceranski, posted on May 24 2012

Late last year, I discovered the Arduino. The Arduino is an open-source physical computing platform based on a simple i/o board and a development environment that implements the Processing/Wiring language. The Processing/Wiring language is a simplified version of the C programming language that has extensions to access the underlying hardware. The Arduino can be used to develop stand-alone interactive objects such as robots, led displays or 3D printers like the MakerBot Replicator.

Since I write software for a living, I am extremely comfortable with the software aspect of the Arduino. However I was a little worried about my lack of electronic skills. Just to give you some background, at the beginning of this project I knew absolutely nothing about electronics. I never used a soldering iron or even held a multimeter. So its goes without saying that I had a lot to learn. I spent a lot of time reading blogs, watching YouTube videos and I even bought the book “Electronics for Dummies”. I typically try to avoid the Dummy books because I find the title to be self deprecating. However in this case, the title fit. Fortunately, after a couple of fun filled weeks, I learned how to solder, felt comfortable using a multimeter and started to understand the basic concepts. I eventually got to the point where I felt I could build something without electrocuting myself! After some brainstorming I decided to build a robot!

The first step in building a robot is to pick a chassis. If you are handy, you can build something from scratch or hack apart an existing toy. In my case, I decided to use a chassis that I found on eBay. The kit I purchased was actually the 2WD Mobile Platform from DFRobot. Buying a pre-built platform has some advantages. Mainly because the kit comes with 2 motors, mounting hardware, wheels, hookup wire and a platform large enough to attach an Arduino, some shields and a small breadboard. The kit also comes with a toggle switch and a six AA battery holder. If you are a beginner like me, then I would recommend buying a kit because it will save you the time of trying to figure out which parts to buy. Finding parts on sites like Mouser or Digi-Key can be intimidating for beginners.

2WD_MobilePlatformKit robot chassis

While I was waiting for my chassis to arrive, I purchased a motor shield kit from Adafruit. There are a variety of motor shields on the market but I settled on the Adafruit board for a few reasons. First of all it has excellent documentation. If you are a newbie like me, then getting a step by step instruction guide with pictures is very important. Second, I know that the Adafruit team stands behind their products. If I run into any issues I can rest assured that someone will help me out. Finally, the shield comes with a software library and a sample sketch. On top of all that, the hardware and software are open source! If you are going to build a robot then I would highly recommend buying this shield.

ada_motorshield

Once you assemble the motor shield you can simply stack it on top of an Arduino board. The male pins from the motor shield plug directly into the female headers of the Arduino board beneath it. The motor shield will occupy quite a few pins. In the end, you will be left with digital pins 2 and 13 and all of the analog pins. The analog pins are conveniently broken out in the bottom right hand corner. The board also gives you easy access to additional +5 volt and ground connections. In order to make prototyping easier and to increase the reusability of the shield, I soldered a 6 pin female header across each of the three rows in the bottom right hand corner of the PCB. I also ended up placing a 2 pin header across digital pins 0 and 1 which are used for TX (transmit) and RX (receive). The TX and RX pins are used for communication to the Arduino, for example when you upload a sketch to the Arduino or when you are sending serial commands from an external device. As a tip, if you have the RX and TX pins wired to a separate device for processing commands, you will need to disconnect these wires before you can upload any sketches.

For phase one of my project, I decided to make my robot autonomous. Autonomous means that the robot can drive around without anyone controlling it. In order for this to work, the robot needs to be able to detect and avoid obstacles. In other words, it needs to be able to see. The easiest way to implement this functionality is to use some sort of distance sensor. After a bit of research I decided to use the Parallax PING))) Ultrasonic sensor. There are cheaper distance sensors available but the PING sensor had sample code and I knew it would be easy to use.

  ping_sensor

The PING sensor was extremely easy to setup. Within minutes of using it, I had the sample sketch working. After I attached it to my robot chassis, I quickly realized that my robot lacked peripheral vision. If an object was not directly in front of the PING sensor the robot would crash into it. I solved this problem by purchasing a pan and tilt bracketfrom Sparkfun. The pan and tit bracket uses two servos which allows for 180 degrees of movement vertically and horizontally. I mounted the PING sensor on top of the bracket and hooked the servo wires to the motor shield. After a little bit of coding, my robot was able to take distance measurements in multiple directions!

 pan_tilt_bracket

Unfortunately, with the addition of the two servos I exceeded the amount of amps that six AA batteries could provide to the motor shield. Six AA batteries will provide you with approximately 9 volts at 1-1.5 amp hours (series gives you more volts but not more amp hours). As a result, I decided to invest in some Lipo batteries. The Lipo batteries are small and light enough for the robot to carry and provide sufficient amperage to drive 2 motors and 2 servos. After doing some price comparisons, I ended up buying my Lipo batteries from Hobby King. I bought one 1300mAh 2S 20C battery to run the Arduino and a 2200mAh 3S 20C to supply power to the motor shield. I also picked up some XT60 connectors to make it easier to connect and disconnect the batteries. Of course, Lipo batteries are useless without a charger so I threw one of those in my cart too. At the end of the day, I ended up spending more money than I wanted too but since the batteries are rechargeable they can be used on other projects.

Once I got the robot to work autonomously I quickly got bored of watching it drive aimlessly around the room. So I decided to add a remote control mode. As a starting point, I found an instructable which used a $10 Bluetooth adapter paired with an Android phone as a remote control. Once paired, the BlueTooth module can be used as a virtual serial port. Then it becomes a simple matter of sending and receiving serial commands in order to make the robot move. In the instructable, the author uses a Python script to send commands, but I decided to use a native Android app instead. So I downloaded the Eclipse IDE and started working my way through the “hello world” Android tutorials. To my surprise, Android development is easy to learn. There is ample documentation and plenty of community support. I also got lucky because the Android SDK includes a BlueTooth Chat application. I ended up using this application as the basis for my remote control app.

In order to switch the robot between autonomous and remote control mode I added a toggle switch to my robot. The toggle switch controls the power to the BlueTooth adapter. I use one of the analog pins on the Arduino to check if the Bluetooth module is active (HIGH) or inactive (LOW). If the BlueTooth module is active I process serial commands, otherwise I run the autonomous code.

The Arduino Code

Here is a brief summary of the Arduino code. The full source code is available on GitHub

The Adafruit motor library is really simple to use. Simply add a reference and use the methods to control the speed and direction of the motors. If you are using servos with the motor shield you can leverage the existing Servo library that comes with the Arduino IDE. Here are some examples of the methods I used to control the motors.

void brake()
{
  leftMotor.run(RELEASE);
  rightMotor.run(RELEASE);
}

void moveForward( int duration = 110 )
{
  leftMotor.run(FORWARD);
  rightMotor.run(FORWARD);
  delay(duration);
}

void moveBackward( int duration = 1000 )
{
  leftMotor.run(BACKWARD);
  rightMotor.run(BACKWARD);
  delay(duration); //amount of time it takes to back up 
}

void turnLeft( int duration = 480)
{
  leftMotor.run(FORWARD);
  rightMotor.run(BACKWARD); //turns off the motor
  delay(duration);
}

void turnRight( int duration = 480 )
{
  leftMotor.run(BACKWARD);//turns off the motor
  rightMotor.run(FORWARD); 
  delay(duration);
}

For processing serial commands, I leveraged the SerialCommand library. The library allows you to map incoming commands with methods in your Arduino sketch. For example, if you send the command “F 1000” to the Arduino my code will run the forwardCommand. The library also gives you easy access to the arguments sent with the command. For example the command “F 1000” has two parts. The command is “F” for “Forward” and the argument is 1000. In my case, this means move forward for 1000 milliseconds or 1 second. Here is the code:

void setupSerial()
{
  Serial.begin(9600);
  sCmd.setTerm(';');
  sCmd.addCommand("F", forwardCommand );
  sCmd.addCommand("f", forwardCommand );
  
  sCmd.addCommand("B", backwardCommand );
  sCmd.addCommand("b", backwardCommand );
  
  sCmd.addCommand("L", turnLeftCommand );
  sCmd.addCommand("l", turnLeftCommand );
  
  sCmd.addCommand("R", turnRightCommand );
  sCmd.addCommand("r", turnRightCommand );
  
  sCmd.addCommand("?", showCommands );
  sCmd.setDefaultHandler( unrecognized );
}

void showCommands()
{
  Serial.println( "Hello Master. I am awaiting your commands." );
  Serial.println( "------------------------------------------" );
  Serial.println( "Commands must be terminated with a semi-colon:" );
  Serial.println( "\tMove (F)orward - F <# of steps>");
  Serial.println( "\tMove (B)ackward - B <# of steps>");
  Serial.println( "\tTurn (R)ight - R <value>");
  Serial.println( "\tTurn (L)eft - L <value>");
}

void unrecognized(const char *command)
{
  Serial.println("Huh?");
}

Here is the code that uses conditional logic to determine whether the robot is in autonomous or remote control mode:

void loop()
{   
  //check if we are using the bluetooth module, if so listen for commands only
  if( digitalRead(btPin) == HIGH ) { 
    sCmd.readSerial();
    return;
  }
  
  //do autononmous functions ....
}

The Android App – Remote Control

Here is a brief summary of the Android code. The full source code is available on GitHub.

image

The UI is pretty simple. It’s basically just a joystick, a few labels and a connection dialog which allows you to choose which BlueTooth device to connect to. For the joystick I used the Mobile Anarchy Widgets. At this point, my app still needs quite a bit of work. However, the basic concept is proven and its just a matter of adding some trim and polish.

Here is the chunk of code that creates the virtual serial connection:

String address = data.getExtras().getString(DeviceListActivity.EXTRA_DEVICE_ADDRESS);
String deviceName = data.getExtras().getString(DeviceListActivity.EXTRA_DEVICE_NAME);
// Get the BLuetoothDevice object
BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);         
try {
    Method m = device.getClass().getMethod("createRfcommSocket", new Class[] {int.class});
    socket = (BluetoothSocket) m.invoke(device, 1);
    socket.connect();
    txtHeader.setText("connected to device: " + deviceName );
    outputStream = socket.getOutputStream();
    //InputStream inputStream = socket.getInputStream();                
} catch (Exception e) {
    txtHeader.setText("connection failed: " + e.getMessage() );
}

When the joystick is moved, a event handler is called. I then look at the location of the joystick and send the appropriate command to the Arduino. I also added a little bit of code to control how often a command is sent. If you send too many commands at once you can overflow the serial port and the app will crash.

private JoystickMovedListener _listener = new JoystickMovedListener() {

Date lastSent = new Date();

@Override
public void OnMoved(int pan, int tilt) {
    txtX.setText(Integer.toString(pan));
    txtY.setText(Integer.toString(tilt));

    if( socket == null || outputStream == null ) return;
    
    //only send a message once a second
    long diff = new Date().getTime() - lastSent.getTime();
    if( diff < 500 ) return;
    lastSent = new Date();
    
    String command = getCommand( pan, tilt );
    if( command.length() == 0 ) return;
    
    byte[] bytes = command.getBytes();
    try {
        outputStream.write(bytes);
        outputStream.flush();
    } catch (IOException e) {                
        e.printStackTrace();
    }                            
}

public String getCommand( int x, int y )
{
    if( ( x >= -3 && x <= 3 ) && y > 3 )
        return "B 1000;";
    
    if( ( x >= -3 && x <= 3 ) && y < -3 )
        return "F 1000;";
    
    if( ( y >= -3 && y <= 3 ) && x < -3 )
        return "L 1000;";
    
    if( ( y >= -3 && y <= 3 ) && x > 3 )
        return "R 1000;";
    
    return "";
}

@Override
public void OnReleased() {
    txtX.setText("released");
    txtY.setText("released");
}

public void OnReturnedToCenter() {
    txtX.setText("stopped");
    txtY.setText("stopped");
};

Introducing “Robie” the Robot

Here is a video of “Robie” the Robot (my kids named him) running in autonomous mode. This video was taken prior to adding the remote control capabilities.

Conclusion

One of the best things about building this robot was that my kids are now interested in electronics. They love to ask me questions about how the robot works and they are extremely curious about the projects that I have sitting on my workbench. For years I have tried to get them interested in engineering but I could never find the right project to motivate them to learn. However, the Arduino seems to have a special appeal towards makers and even children. As proof, here is a picture that my daughter Noelle drew of her and I working on “Robie”.

Noelles_Drawing_of_Robie

Since I started working with the Arduino I have really gained confidence in my ability as a maker. In the past, when something electronic would break around the house I would either throw it away or send it out for repair. Nowadays, I attempt to fix things myself. Once you consider yourself a maker, you start to think differently. After all, “If You Can’t Open It, You Don’t Own It”

ninja star Using PowerShell to Publish a NuGet Package

by Michael Ceranski, posted on May 07 2012

At my employer we have a local NuGet server to host all of our internal packages. Occasionally, I’ll be working on a project and realize that I need to tweak something in one of  my NuGet packages. Initially, I got into the habit of  opening up a second instance of Visual Studio, making the necessary changes and using the NuGet web interface to re-upload the package. I quickly realized that manually uploading the package was too time consuming. Therefore, I started looking for a way to automate the process instead. Eventually that led me to the PowerShell script you see below.

$nugetServer = "https://<your nuget server here>"
$apiKey = "<your api key here>"
$packageName = "<your package name here>"

$latestRelease = nuget list $packageName
$version = $latestRelease.split(" ")[1];

$versionTokens = $version.split(".")
$buildNumber = [System.Double]::Parse($versionTokens[$versionTokens.Count -1]) 
$versionTokens[$versionTokens.Count -1] = $buildNumber +1
$newVersion = [string]::join('.', $versionTokens)
echo $newVersion

get-childitem | where {$_.extension -eq ".nupkg"} | foreach ($_) {remove-item $_.fullname}
nuget pack -Version $newVersion
$package = get-childitem | where {$_.extension -eq ".nupkg"}
nuget push -Source $nugetServer $package $apiKey


The script needs a few variables defined in order for it to run. The first variable ($nugetServer) is the URL of the NuGet Server. The second variable ($apiKey) is your personal API key. You can get your API key by logging into your NuGet Server with a browser. After you log in, click on your username in the upper right hand corner. This will take you to your account page. On the bottom of the “My Account” page there is a box which you can click on to make your API key visible. Finally the last variable ($packageName) is the name of the package you are uploading. This can be easily acquired by looking at your project properties and copying the Assembly name from the Application tab.

Depending on how your machine is configured you may have the option to Run with PowerShell on your context menu. If not, you can take a look at this blog post in order to configure it manually. Alternatively you can use the following command instead.

powershell.exe "<path to the script>\publish.ps1"


If you have any problems running the script then please refer to the following TechNet article or send me a question and I’ll be glad to help.

Tags:

ninja star jQuery + CSS3 Media Queries = Dynamic UX

by Michael Ceranski, posted on April 18 2012

I have been working on a project where I render an HTML table to display some information about a project. When the table is displayed on a wide monitor I can comfortably fit about 10-15 columns. However, when viewing the same page on a screen with less real-estate things get crowded. Most people would probably address this problem by creating a mobile version of the site. In many cases this is the proper solution if you really want to tailor the user experience for the device. However, in my case I just wanted to toggle the visibility of some of the columns in the table based on the screen size and I really did not want to get into the hassle of maintaining a separate mobile interface. Therefore I came up with a solution based on jQuery and CSS Media Queries. Below is a screenshot that shows how columns are being toggled on and off based on the width of the browser.

css_media_queries

So let me start this tutorial by showing you the markup that I used to generate the HTML table. If you are not a MVC/Razor developer then this part of the tutorial may not be useful to you. However I wanted to include this extra step because the Razor grid’s Html Helper has some limitations which I addressed with jQuery. 

@{ 
    var grid = new WebGrid( 
        canPage: true,
        rowsPerPage: Model.PagingInfo.PageSize,       
        ajaxUpdateContainerId: "container",
        ajaxUpdateCallback: "gridLoaded"
    );
    
    grid.Bind(Model.Data, rowCount: Model.PagingInfo.TotalItems, autoSortAndPage: false);    
       
    <div id="container">
        @grid.GetHtml(
            tableStyle: "webGrid",
            headerStyle: "header",
            alternatingRowStyle: "alt",
            htmlAttributes: new { id = "grid", page = Model.PagingInfo.PageNumber },
            columns: grid.Columns(
                grid.Column("Title", "Title"),
                grid.Column("StartDate", "Start"),
                grid.Column("EndDate", "End"),
                grid.Column("PercentComplete", "%"),

                grid.Column("Status", "Status", style: "xtra"),
                grid.Column("Notes", "Notes", style: "xtra"),
                grid.Column("MetaData", "MetaData", style: "xtra"),
                grid.Column("MoreMetaData1", "More MetaData", style: "xtra"),
                grid.Column("EvenMoreMetaData1", "Even More MetaData", style: "xtra")
            )
        )
    </div>     
}

As you can see, the razor grid allows you to specify styles for each column (TD), so I added the style class "xtra" to the fields that I want to hide when the screen with is 800 pixels or less. Unfortunately the (TH) columns can not be styled directly with the Html Helper. Therefore I needed a way to apply the “xtra” class to the headers that correspond with each column. This was very easy to do with jQuery:

$("#grid tbody tr:first td.xtra").each(function (index,td) {                
    $('#grid thead th').eq($(td).index()).addClass("xtra");                
});

 

The snippet above uses a selector to get at the first row of the table. It then loops through all of the columns in that row that have the "xtra" style applied to it. Then for each column it finds the corresponding header and adds the "xtra" style to it. This is important because if we did not apply the style to the headers then our CSS below would only hide the columns and not the headers.

Now let's move on to adding the CSS media queries to our style sheet. If you are unfamiliar with media queries then here is a quick definition that I found on the W3C site:

Media queries extend the functionality of media types by allowing more precise labeling of style sheets. A media query consists of a media type and zero or more expressions that check for the conditions of particular media features. Among the media features that can be used in media queries are ‘width’, ‘height’, and ‘color’. By using media queries, presentations can be tailored to a specific range of output devices without changing the content itself.

So in simple terms, we can have sections in our style sheet that only apply when the screen is a certain size. In my particular case I decided to hide the columns  with the “xtra” style applied to them whenever the screen width is less than 800 pixels:

@media screen and (max-width:800px) { 
  .webGrid .xtra { display: none;}  
  .webGrid .xtra > th { display: none;}    
}

Keep in mind that you can also do things like adjust fonts, remove background images and hide navigation panels. This can help make the user experience better when dealing with smaller screens. Although this trick is not a replacement for a well crafted mobile interface it certainly gives you a quick way to make your site look good on smaller screens.

Happy Coding!

ninja star INotifyPropertyChanged with Compile Time Checking

by Michael Ceranski, posted on March 27 2012

Update: An anonymous reader left a great tip! ".NET 4.5 gives you the new Caller Info attributes, which would remove the need to actually pass in the name of the property, if you're raising the notification from withing the property itself." For more details read this blog post.

Once you start doing WPF MVVM development you will quickly grow tired of implementing the INotifyPropertyChanged interface. So in order to preserve my sanity, I create a base class which implements the INotifyPropertyChanged interface. The first version of my class was defined as:

public class NotifyPropertyChangedBase : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged = delegate { };

    protected void RaisePropertyChanged(string propertyName)
    {
        PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
    }
}

This is about as basic as an implementation of INotifyPropertyChanged can get. To use it, you just implement the class and call RaisePropertyChanged. Here is an example:

public class MyViewModel : NotifyPropertyChangedBase
{
    private string _name;
    public string Name {
      get{ return _name; }
      set{ 
       _name = value;
       RaisePropertyChanged("Name");
      }
    }
}

After using this version of the code for a couple of days I quickly realized that using a lambda to get the property name would be less error prone than using a string. So in version 2 of the code, I added lambda support.

By the way, the ExtractPropertyName method you see below, was copied verbatim from the Prism project. Prism and MVVM Light Toolkit both take care of wrapping the INotifyPropertyChanged interface.  I personally do not use either framework because I feel that they are both too bloated for my purposes. After a bit of contemplation, I ended up building my own MVVM framework, which I have packaged up and placed on my private NuGet server. However, if you are new to .NET or MVVM then I would suggest starting with a framework first. In any case, here is the updated code:

protected void RaisePropertyChanged<T>(Expression<Func<T>> propertyExpression)
{
    var propertyName = ExtractPropertyName(propertyExpression);
    RaisePropertyChanged(propertyName);
}

protected string ExtractPropertyName<T>(Expression<Func<T>> propertyExpression) {
    if (propertyExpression == null) {
        throw new ArgumentNullException("propertyExpression");
    }

    var memberExpression = propertyExpression.Body as MemberExpression;
    if (memberExpression == null) {
        throw new ArgumentException("The expression is not a member access expression.", "propertyExpression");
    }

    var property = memberExpression.Member as PropertyInfo;
    if (property == null) {
        throw new ArgumentException("The member access expression does not access a property.", "propertyExpression");
    }

    if (!property.DeclaringType.IsAssignableFrom(this.GetType())) {
        throw new ArgumentException("The referenced property belongs to a different type.", "propertyExpression");
    }

    var getMethod = property.GetGetMethod(true);
    if (getMethod == null) {
        // this shouldn't happen - the expression would reject the property before reaching this far
        throw new ArgumentException("The referenced property does not have a get method.", "propertyExpression");
    }

    if (getMethod.IsStatic) {
        throw new ArgumentException("The referenced property is a static property.", "propertyExpression");
    }

    return memberExpression.Member.Name;
}        

With the lambda version of RaisePropertyChanged in place, I can now get compile time checking. Compile time checking is great if you have fat fingers or if you do a lot of refactoring. Let’s take a look at our updated ViewModel:

public class MyViewModel : NotifyPropertyChangedBase
{
    private string _name;
    public string Name {
      get{ return _name; }
      set{ 
       _name = value;
       RaisePropertyChanged(() => Name);
      }
    }
}

I also added an overloaded version of the RaisePropertyChanged method so I could raise multiple properties using a single method call. This is useful when you have two of more related properties. 

protected virtual void RaisePropertyChanged(params string[] propertyNames) {
    foreach( var property in propertyNames ) {
        RaisePropertyChanged(property);
    }
} 

Unfortunately, I could not figure out how to make an overloaded method that takes multiple lambdas. Because the type that represents "T" needs to be different for each expression I struggled to find any syntactical sugar that would make it work. To clarify the problem, I made an overloaded version of the method that takes two lambda expressions. You will notice that T and X are different types. T may be an integer and X may be an observable collection. 

protected void RaisePropertyChanged<T,X>(Expression<Func<T>> one, Expression<Func<X>> two ) 
    RaisePropertyChanged(one);            
    RaisePropertyChanged(two);            
}

With this method you could use the following syntax. Of course, with my sample method above you are limited to two parameters only:

RaisePropertyChanged( () => CountryId, () => States );

If you have a solution for making an overloaded method with params and lambdas then please leave a comment.

Moving on...The classic example of two related properties are Country and State. When you change the value of the country drop down, you want the corresponding states or provinces to appear. Here is an example:

public class AddressViewModel : NotifyPropertyChangedBase
{
     private Address Model { get; private set; }
     
     public AddressViewModel( Address model )
     {
         Model = model;
     }

     public int Country
     {
         get { return Model.CountryId; }
         set
         {
             Model.CountryId = value;
             RaisePropertyChanged( () => CountryId);
             RaisePropertyChanged( () => States);
         }
     }

     public ObservableCollection<Country> Countries {
         ...
     }

     public ObservableCollection<State> States
     {
         ...
     } 

     public int StateId
     {
         ...
     }
}

To summarize, if you find yourself implementing INotifyPropertyChanged a lot then I would highly recommend moving that functionality into a base class. In addition, using the lambda version of the RaisePropertyChanged event can provide you with compile time checking which is always a great thing to have!

Tags: ,

About the author

MikeMichael Ceranski is a developer specializing in the .NET stack. I have spent time as a DBA, Web Developer and even a network engineer. Up til now most of my career has revolved around the .NET stack but I have recently taken an interest in microcontrollers which has forced me to get acquainted with lower level languages such as C, and C++.

View my resume

Sponsors