EnumEditBox using UseDisplayAttributes no initial value displayed

Editors for WPF Forum

Posted 11 months ago by John Kohler
Version: 22.1.4
Avatar

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).

Suggestions?

XAML:

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

Example Enumeration:

public enum SmoothingFilterType
{
    [Display( Name = "Canny" )]
    canny,
    [Display( Name = "Deriche 1" )]
    deriche1,
    [Display( Name = "Deriche 2" )]
    deriche2,
    [Display( Name = "Lanser 1" )]
    lanser1,
    [Display( Name = "Lanser 2" )]
    lanser2,
    [Display( Name = "Shen" )]
    shen,
    [Display( Name = "MShen" )]
    mshen,
    [Display( Name = "Sobel" )]
    sobel,
    [Display( Name = "Sobel Fast" )]
    sobel_fast
}

Comments (4)

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

Hello,

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 10 months ago]


Actipro Software Support

Posted 10 months ago by John Kohler
Avatar

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"
HorizontalAlignment="Center"
MinWidth="250"
PlaceholderText="Enum"
IsNullAllowed="False"
EnumType="{Binding EnumWithFlagsType}"
Value="{Binding EnumWithFlagsCurrentValueAsString}"
UseDisplayAttributes="True"/>

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 10 months ago by Actipro Software Support - Cleveland, OH, USA
Avatar

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 10 months ago by John Kohler
Avatar

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

JK

The latest build of this product (v24.1.3) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.