0

I'm sorry to ask a really basic question but am working on having a converter in code behind xaml page. This converter would change a background color based on the value in a data element. I know the data element I want is available because when I break debugging at the execution of the converter, the value I want is in "value". From the immediate window in VS2013 I display the contents of "value" and get this:

{VehicleTracks.ViewModels.vehicleViewModel}
base: {VehicleTracks.ViewModels.vehicleViewModel}
purchasedate: {7/14/2014 12:00:00 AM}
PurchaseDate: {7/14/2014 12:00:00 AM}
VehColor: "Chartreuse"
VehicleBitMap: null
vehiclebitmap: null
vehiclecolor: "Chartreuse"
vehiclemodel: "Algo"
vehmake: "Test Vehicle"
VehMake: "Test Vehicle"
VehType: "Auto"
vehtype: "Auto"
vehyear: "1908"
VehYear: "1908"

The element "VehType" is the one I want. I want to change background color based on its value. You see that it is set to "Auto" in this instance. Here is the applicable part of my converter code:

    public sealed class ValueToColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            SolidColorBrush blueBrush = new SolidColorBrush(Windows.UI.Colors.Blue);
            if (value[VehType] == "Auto")
            {
            return blueBrush;
            }
            else
            {
                SolidColorBrush greenBrush = new SolidColorBrush(Windows.UI.Colors.Green);
                return greenBrush;
            }
    }

The reference I now have there, value[VehType] is not getting it. What do I need there?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
wheezer
  • 135
  • 1
  • 2
  • 9
  • I have edited your title. Please see, "[Should questions include “tags” in their titles?](http://meta.stackexchange.com/questions/19190/)", where the consensus is "no, they should not". – John Saunders Jul 09 '14 at 15:47

1 Answers1

3

You're misusing indexers there, here's an article to help explain: Using Indexers in C#

Can you not just cast it to VehicleTracks.ViewModels.vehicleViewModel?

using VehicleTracks.ViewModels;

// -----

var viewModel = (vehicleViewModel) value;
if (viewModel.VehType == "Auto")
{
    return blueBrush;
}
Daniel May
  • 8,156
  • 1
  • 33
  • 43
  • Thank you so much. I guess I showed that I barely half know what I'm doing but your solution was right on target! – wheezer Jul 09 '14 at 15:09