Bind Icon depending on Enum in WPF Treeview

前端 未结 3 871
后悔当初
后悔当初 2020-12-18 19:24

I have at treeview TextBox, and I want convert my Enum:



        
3条回答
  •  遥遥无期
    2020-12-18 20:00

    Create a Value Converter

    It takes your enum value and returns the filename of the appropriate icon.

    [ValueConversion(typeof(AcceptationStatusGlobalFlag), typeof(string))]
    public class AcceptationStatusGlobalFlagToIconFilenameConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            switch ((AcceptationStatusGlobalFlag)value)
            {
                case AcceptationStatusGlobalFlag.Ready:
                    return "ready.jpg";
                case AcceptationStatusGlobalFlag.NotReady:
                    return "notready.jpg";
                case AcceptationStatusGlobalFlag.AcceptedByAdmin:
                    return "AcceptedByAdmin.jpg";
                default:
                    return null;
            }
    
            // or
            return Enum.GetName(typeof(AcceptationStatusGlobalFlag), value) + ".jpg";
        }
    
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotSupportedException();
        }
    }
    

    You will need to add a reference to this converter in your XAML

    
        
            
        
    

    Replace your TextBlock

    
    

    with an Image and tell it use your converter

    
    

提交回复
热议问题