1

I have been trying to make this work for a while with no success.
I have a converter to pretty print dates on the page. Convert function is as follows;

class DateConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, string language)
    {
        Nullable<int> _date = value as Nullable<int>;

        if (!_date.HasValue)
            return DependencyProperty.UnsetValue;

        if (!String.IsNullOrEmpty(parameter as String))
        {
            if (parameter.Equals("EDITED"))
                return "edited " + UtilityFunctions.formatUnixTime(_date.Value);
        }

        return UtilityFunctions.formatUnixTime(_date.Value);
    }
}

In XAML part I am using this converter by passing a property of a property to it.

<TextBlock Text="{Binding Wiki.EditDate, Converter={StaticResource DateConverter}, ConverterParameter=EDITED}" />

My ModelView extends BindableBase and whenever I update Wiki property, I call SetProperty which in return calls OnPropertyChanged for Wiki property.

If I try to show the date without a converter, it works fine.

<TextBlock Text="{Binding Wiki.EditDate}" />

I am using this converter in other parts of my projects so I believe it is not the cause of the problem.

What might be the cause of this problem?

Thanks for any helps...

Birkan Cilingir
  • 488
  • 4
  • 15
  • Have you tried to debug and check what happens? Have you defined resource with a key *DateConverter*, ``? Have you looked at output window while debugging - are there any exceptions? – Romasz Dec 20 '14 at 20:29
  • @Romasz Yes, I have debugged the converter and the value of _date is null. There are no binding errors in the output console. Resource is also defined, and I am currently using this converter on another page. The difference is on this page I am loading the value from a web API using an async method. So when the page opens the value is not set yet. – Birkan Cilingir Dec 21 '14 at 00:22
  • 1
    So this means that the breakpoint in the converter is being hit, is that right? Can you check what's the value of `value` not `_data`? – Romasz Dec 21 '14 at 05:58
  • @Romasz thanks for your feedback. The value was formatted ("2014-06-03 00:56:21") and was not an int hence the conversion to Nullable failed. Now the question is why didn't the conversion throw an exception? By the way, can you write this as an answer with some explanation so that I can mark it as an answer. Thanks for your help. – Birkan Cilingir Dec 21 '14 at 10:38
  • In the answer I've explained why convecrsion doesn't throw an extension. – Romasz Dec 21 '14 at 11:27

1 Answers1

0

As it turned out in discussion and debugging - the problem was that the value passed to converter was wrong - "2014-06-03 00:56:21".

Such value can't be coverted to int, and because you are using as:

Nullable<int> _date = value as Nullable<int>;

there won't be exception thrown. Instead of exception you have null (as doesn't throw InvalidCastException). If you would use cast:

Nullable<int> _date = (Nullable<int>)value;

then you would have an exception. Some more information you will surely find on SO - for example this answer.

Community
  • 1
  • 1
Romasz
  • 29,662
  • 13
  • 79
  • 154