How can I use enum types in XAML?

后端 未结 2 1684
庸人自扰
庸人自扰 2020-12-05 02:26

I\'m learning WPF and I encountered the following problem:

I have an enum type in another namespace than my XAML:

 public enum NodeType
 {
    Type_SYS         


        
2条回答
  •  天涯浪人
    2020-12-05 03:04

    I had a similar question here, and my end result was to create a generic IValueConverter that passed the enum value I wanted to match in as the ConverterParameter, and it returns true or false depending on if the bound value matches the (int) value of the Enum.

    The end result looks like this:

    XAML Code:

    
    

    Converter

    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        if (parameter == null || value == null) return false;
    
        if (parameter.GetType().IsEnum && value is int)
        {
            return (int)parameter == (int)value;
        } 
        return false;
    }
    

提交回复
热议问题