EnumEditBox using UseDisplayAttributes no initial value displayed

Editors for WPF Forum

Posted 7 months ago by John Kohler
Version: 22.1.4

When using EnumEditBox with UseDisplayAttributes="True", the box initially show as empty - that is, no value is initially displayed.  The popup list shows correctly but there's no way to tell what the current value is without actually setting it via the popup.

The value being bound to the edit box is a string (the enum value name).

Other than the initially displayed value being empty, the control works as expected. 

With the UseDisplayAttributes set to false, the initially displayed value is correct (just kinda ugly).



<DataTemplate x:Key="EUEnumEditBox">
    <editors:EnumEditBox Value="{Binding Value,Mode=TwoWay}"
        EnumType="{Binding EnumType}"
        UseDisplayAttributes="True" />

Example Enumeration:

public enum SmoothingFilterType
    [Display( Name = "Canny" )]
    [Display( Name = "Deriche 1" )]
    [Display( Name = "Deriche 2" )]
    [Display( Name = "Lanser 1" )]
    [Display( Name = "Lanser 2" )]
    [Display( Name = "Shen" )]
    [Display( Name = "MShen" )]
    [Display( Name = "Sobel" )]
    [Display( Name = "Sobel Fast" )]

Comments (4)

Posted 6 months ago by Actipro Software Support - Cleveland, OH, USA


I went into our EnumEditBox Intro QuickStart sample and updated line 60 to be:

<editors:EnumEditBox x:Name="editBox" HorizontalAlignment="Center" MinWidth="250" PlaceholderText="Enum" UseDisplayAttributes="True"
	IsNullAllowed="False" EnumType="{Binding EnumWithFlagsType}" Value="{Binding EnumWithFlagsCurrentValue}"  />

The enum being bound there has DescriptionAttribute (same effective thing as DisplayAttribute with Name) and when I run the sample, it shows "Nada" (the description) for the zero value, which is expected.  Can you tell us how to alter that sample to make the problem occur?

Also you might want to be on the latest 23.1 version when checking in case this is something that was already resolved.

[Modified 6 months ago]

Actipro Software Support

Posted 6 months ago by John Kohler

I was able to recreate my issue in your SampleBrowser:

0) Installed WPF Controls 23.1.3 and am using VisualStudio 2022

1) As the enum I'm working with is not attributed with Flags, I  commented out the Flags attribute on the EnumWithFlags enumeration  @ EnumWithFlagc.cs line:10  

2) My target property is of type string and is always initialized, so I added an initialized string property to the class ActiproSoftware.ProductSamples.EditorsSamples.QuickStart.EnumEditBoxIntro.MainControl @ line 60:

public string EnumWithFlagsCurrentValueAsString { get; set; } = EnumWithFlags.Three.ToString();

3) Changed the binding on the EnumEditBox to the new string property:

<editors:EnumEditBox x:Name="editBox"
EnumType="{Binding EnumWithFlagsType}"
Value="{Binding EnumWithFlagsCurrentValueAsString}"

Now the edit box initially shows with the 'PlaceholderText' instead of the description of the initialized property value (should be "an odd number").

Interestingly, toggling the 'Use display attributes' checkbox, toggles the displayed value between the enum value (Three) and the PlaceholderText (Enum).  I expect it to toggle between 'Three' and 'An odd number'.

Answer - Posted 6 months ago by Actipro Software Support - Cleveland, OH, USA

Hi John,

Thanks for the additional detail.  I think the problem here is that you are binding a string (in this case the string "Three") into the EnumEditBox.Value property.  But the EnumEditBox.Value property takes an enumeration item value (but has to be declared an Object for various enum types to work) and is not meant to take a string, so no match is found.  If you want to bind to a string, you really need to create a value converter that could be added to that EnumWithFlagsCurrentValueAsString binding.  Also, if you did add a value converter to work with display attributes, I imagine you'd want the EnumWithFlagsCurrentValueAsString default value to be "An odd number" instead of "Three".

As for when the "Use display attributes" checkbox is toggled, it's showing something in that one case but the same problem exists as above, where Value isn't designed to bind to a string.  It needs to be bound to an enum item value.  It's only showing "Three" there because you passed that as the string.  If you passed "Foo", it would show "Foo".

To summarize, if you want to bind a string to the EnumEditBox.Value property, you will need to use a value converter.

Actipro Software Support

Posted 6 months ago by John Kohler

Thank you for the clarification.  When time allows, I'll take the converter route.


The latest build of this product (v24.1.2) was released 2 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.