aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/FSE/Tango.FSE.Common/Converters/DisplayMemberPathToStringConverter.cs
blob: 65714916a36d9ca48a4644689ad85710d4881411 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Data;
using Tango.Core.ExtensionMethods;

namespace Tango.FSE.Common.Converters
{
    public class DisplayMemberPathToStringConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            try
            {
                if (values[0] != null && values[1].ToStringSafe() != null && values[1] != DependencyProperty.UnsetValue)
                {
                    try
                    {
                        if (values[0].GetType().IsEnum && values[1].ToStringSafe() == "Description")
                        {
                            return ((Enum)values[0]).ToDescription();
                        }
                    }
                    catch { }

                    return values[0].GetPropertyValueByPath(values[1].ToStringSafe());
                }
                else
                {
                    return values[0].ToStringSafe();
                }
            }
            catch
            {
                return values[0].ToStringSafe();
            }
        }

        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
}