views:

262

answers:

2

I'm using MVVM (MVVM Light Toolkit) and have a property on the view model which exposes a list of objects. The objects contain two properties, both strings, which correlate to an abbreviation and a description. I want the ComboBox to expose the pairing as "abbreviation - description". If I use a data template, it does this easily.

I have another property on the view model which represents the object that should display as selected -- the chosen item in the ComboBox. I'm binding the ItemsSource to the list, as it represents the universe of available selections, and am trying to bind the SelectedItem to this object. I'm killing myself trying to figure out why I can't get it to work, and feeling more like a fraud by the hour.

In trying to learn why this works, I created the same approach with just a list of strings, and a selected string. This works perfectly. So, it clearly has something to do with the typing... perhaps something in choosing equality? Or perhaps it has to do with the data template?

Here is the XAML:

<Window x:Class="MvvmLight1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"
        Height="300"
        Width="300"
        DataContext="{Binding Main, Source={StaticResource Locator}}">

    <Window.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Skins/MainSkin.xaml" />
            </ResourceDictionary.MergedDictionaries>
            <DataTemplate x:Key="DataTemplate1">
                <StackPanel Orientation="Horizontal">
                    <TextBlock TextWrapping="Wrap" Text="{Binding CourtCode}"/>
                    <TextBlock TextWrapping="Wrap" Text=" - "/>
                    <TextBlock TextWrapping="Wrap" Text="{Binding CourtDescription}"/>
                </StackPanel>
            </DataTemplate>
        </ResourceDictionary>
    </Window.Resources>

    <Grid x:Name="LayoutRoot">
        <ComboBox x:Name="cmbAbbrevDescriptions" Height="35" Margin="25,75,25,25"  VerticalAlignment="Top" ItemsSource="{Binding Codes}" ItemTemplate="{DynamicResource DataTemplate1}" SelectedItem="{Binding selectedCode}" />
        <ComboBox x:Name="cmbStrings" Height="35" Margin="25" VerticalAlignment="Top" ItemsSource="{Binding strs}" SelectedItem="{Binding selectedStr}"/>
    </Grid>
</Window>

And, if helpful, here is the ViewModel:

using GalaSoft.MvvmLight;
using MvvmLight1.Model;
using System.Collections.Generic;

namespace MvvmLight1.ViewModel
{
    public class MainViewModel : ViewModelBase
    {
        public const string CodesPropertyName = "Codes";
        private List<Court> _codes = null;
        public List<Court> Codes
        {
            get
            {
                return _codes;
            }

            set
            {
                if (_codes == value)
                {
                    return;
                }

                var oldValue = _codes;
                _codes = value;

                // Update bindings and broadcast change using GalaSoft.Utility.Messenging
                RaisePropertyChanged(CodesPropertyName, oldValue, value, true);
            }
        }

        public const string selectedCodePropertyName = "selectedCode";
        private Court _selectedCode = null;
        public Court selectedCode
        {
            get
            {
                return _selectedCode;
            }

            set
            {
                if (_selectedCode == value)
                {
                    return;
                }

                var oldValue = _selectedCode;
                _selectedCode = value;

                // Update bindings and broadcast change using GalaSoft.Utility.Messenging
                RaisePropertyChanged(selectedCodePropertyName, oldValue, value, true);
            }
        }

        public const string strsPropertyName = "strs";
        private List<string> _strs = null;
        public List<string> strs
        {
            get
            {
                return _strs;
            }

            set
            {
                if (_strs == value)
                {
                    return;
                }

                var oldValue = _strs;
                _strs = value;

                // Update bindings and broadcast change using GalaSoft.Utility.Messenging
                RaisePropertyChanged(strsPropertyName, oldValue, value, true);
            }
        }

        public const string selectedStrPropertyName = "selectedStr";
        private string _selectedStr = "";
        public string selectedStr
        {
            get
            {
                return _selectedStr;
            }

            set
            {
                if (_selectedStr == value)
                {
                    return;
                }

                var oldValue = _selectedStr;
                _selectedStr = value;

                // Update bindings and broadcast change using GalaSoft.Utility.Messenging
                RaisePropertyChanged(selectedStrPropertyName, oldValue, value, true);
            }
        }


        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel()
        {
            Codes = new List<Court>();

            Court code1 = new Court();
            code1.CourtCode = "ABC";
            code1.CourtDescription = "A Court";

            Court code2 = new Court();
            code2.CourtCode = "DEF";
            code2.CourtDescription = "Second Court";

            Codes.Add(code1);
            Codes.Add(code2);


            Court code3 = new Court();
            code3.CourtCode = "DEF";
            code3.CourtDescription = "Second Court";
            selectedCode = code3;

            selectedStr = "Hello";
            strs = new List<string>();
            strs.Add("Goodbye");
            strs.Add("Hello");
            strs.Add("Ciao");

        }
    }
}

And here is the ridiculously trivial class that is being exposed:

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

namespace MvvmLight1.Model
{
    public class Court
    {
        public string CourtCode { get; set; }

        public string CourtDescription { get; set; }
    }
}

Thanks!

A: 

Your SelectedCode Code3 is not present in the Codes collection. Add it so that the selection would work as expected.

Your selectedStr works because it is in the collection. [strs.Add("Hello");]

Veer
or override the equals method in Court class, remember also to override the GetHashCode method.
Oggy
@Veer Right, code3 is not present in the Codes collection. But it is equivalent to code2... and that's where I'm missing the boat. I'm trying to say "of all of these possible types, select the one type that matches this one".
Mike L
@Oggy - If I understand, you're saying the ComboBox is trying to set the selected item, but it can't determine equality between object instances?
Mike L
yes. i'll make an answer.
Oggy
+1  A: 

The runtime doesn't know that code2 and code3 should be equal.

see http://msdn.microsoft.com/en-us/library/ms173147(VS.80).aspx

public override bool Equals(object o)
{
   var court = o as Court;
   if(court == null)
      return false;
   return CourtCode == court.CourtCode;
}
Oggy
Awesome! It had nothing to do with WPF, binding, etc. It was just an stupid mistake around type equality. Thanks tons!
Mike L