Categories: Blog

Silverlight ComboBoxes and the Importance of Equals

So I was struggling for awhile with a binding issue in my Silverlight application, and learned the hard way that I forgot my basics.

The scenario is fairly common. I have a view model that contains the entity I want to edit along with supporting lists. The common example I see on the web is something like this:

public class CityEntity 
{
   public int ID { get; set; }
   public string Name { get; set; }
}

public class MyEntity 
{
    public CityEntity CurrentCity { get; set; }
}

public class ViewModel 
{
   public MyEntity Entity { get; set; }
   public ObservableCollection<CityEntity> Cities { get; set; }
}

This is purely contrived but you get the point … I have basic “building block” entities that are composed into the entity I wish to edit, so my view model hosts that entity as well as some collections for binding to drop downs so I can change properties on the entity.

My ComboBox (I thought) was straightforward:

...
<ComboBox Style="{StaticResource SimpleComboBoxStyle}" 
   ItemsSource="{Binding Cities}" 
   SelectedItem="{Binding Path=Entity.CurrentCity,Mode=TwoWay}" DisplayMemberPath="Name"/> 
.. 

Imagine my chagrin when I’d pop up my window and the combo box … never … showed … the city. What was wrong? I had the right path, the right selected item. Was I missing something?

It turns out, I was.

The framework deals with the lists and bindings as objects, so unless the actual reference to the city on the main entity matches the entity in the list, there is no way to “set” the selected item (the framework doesn’t know I intend for them to match).

The solution was quite simple: most of my entites on the Silverlight side derive from a base class, call it SimpleEntity. By implementing Equals (and by way of that, the hash code as well), the framework can now understand how to take my entity over here and match it to the entity in the list over there. Once it was implemented, I pushed it out and voila! my combo boxes started populating with the right value.

Jeremy Likness

Recent Posts

8-Step AWS to Microsoft Azure Migration Strategy

Microsoft Azure and Amazon Web Services (AWS) are two of the most popular cloud platforms.…

3 weeks ago

How to Navigate Azure Governance

 Cloud management is difficult to do manually, especially if you work with multiple cloud…

4 weeks ago

Why Azure’s Scalability is Your Key to Business Growth & Efficiency

Azure’s scalable infrastructure is often cited as one of the primary reasons why it's the…

1 month ago

Unlocking the Power of AI in your Software Development Life Cycle (SDLC)

https://www.youtube.com/watch?v=wDzCN0d8SeA Watch our "Unlocking the Power of AI in your Software Development Life Cycle (SDLC)"…

2 months ago

The Role of FinOps in Accelerating Business Innovation

FinOps is a strategic approach to managing cloud costs. It combines financial management best practices…

2 months ago

Azure Kubernetes Security Best Practices

Using Kubernetes with Azure combines the power of Kubernetes container orchestration and the cloud capabilities…

2 months ago