1

I am trying to accomplish this: Add a Image in the DataGridTemplateColumn but instead of Image use materialDesign:PackIcon. Here is my code.

<DataGridTemplateColumn Header="Message">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <materialDesign:PackIcon Kind="{Binding MsgStatus,Converter={StaticResource NumberToIconConverter}}" Foreground="Green"/>
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

MsgStatus is the column value, depending on its value the icon will be different. This is accomplished through a converter which returns IconKind.

public class NumberToIconConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string input = value as string;
           
            switch (input)
            {
                case "1":
                    return MaterialDesignThemes.Wpf.PackIconKind.Alarm;
                case "2":
                    return MaterialDesignThemes.Wpf.PackIconKind.Message;
                default:
                    return DependencyProperty.UnsetValue;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {            
            throw new NotSupportedException();
        }
    }

Converter is referenced in the usercontrol but it seems that is never called and there is no errors during debugging or execution.

<UserControl.Resources>
        <local:NameToBrushConverter x:Key="NumberToIconConverter"/>
</UserControl.Resources>

Using the same binding with other converters on the same column with Image or textblock (e.g.) works but with materialDesign:PackIcon the converter is never called and thus, icons don't change. Am I wrong with the binding?

EDIT:

Solved: Converter was not being called, replaced reference with:

<UserControl.Resources>
        <local:NumberToIconConverter x:Key="NumberToIconConverter"/>
</UserControl.Resources>

Converter using answer, added DBNull check to avoid Null Exception:

public class NumberToIconConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == DBNull.Value)
            {
                return DependencyProperty.UnsetValue;
            }
            var input = System.Convert.ToInt16(value);

            switch (input)
            {
                case 1:
                    return MaterialDesignThemes.Wpf.PackIconKind.Alarm;
                case 2:
                    return MaterialDesignThemes.Wpf.PackIconKind.Message;
                default:
                    return DependencyProperty.UnsetValue;
            }
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {            
            throw new NotSupportedException();
        }
    }
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
4lex
  • 49
  • 10

1 Answers1

1

I think the problem is in your converter which you cast int to string. I create a sample project with your codes and this model:

public class Messages
{
    public int MsgStatus { get; set; }
}

and i changed your convert to this (look at line 2):

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    var input = System.Convert.ToInt16(value);

    switch (input)
    {
        case 1:
            return MaterialDesignThemes.Wpf.PackIconKind.Alarm;
        case 2:
            return MaterialDesignThemes.Wpf.PackIconKind.Message;
        default:
            return DependencyProperty.UnsetValue;
    }
}

and it's work for me. Output:

enter image description here

AmRo
  • 833
  • 1
  • 8
  • 19
  • I don't know why is still nor working for me, I think it has to be with the binding I've put a break point on the converter and it never reaches that point. It's like MsgStatus does not exist in the datacontex of the datatemplate. – 4lex Sep 08 '19 at 11:00
  • Thanks I'll mark it as the answer, also the reason why converter was not being called was i was referencing another converter NameToBrushConverter to be precise. – 4lex Sep 08 '19 at 17:05