Enum Editor (what did i just do??)

Grids for WPF Forum

Posted 8 years ago by jeff jarrell
Version: 11.1.0543
Avatar
I wanted to derive the enumEditBox for use in the propertyGrid. I added a couple shortcut keys and a .GotFocus and an ErrorTemplate. Now selection doesn't work with the mouse, short-cuts are nice though. still works if i type. I did put a hack in to keep it working in the meantime (see previewMousedown).

If I change the PropGrid:EnumEditorEx to the normal actipro:EnumEditBox (and change the style reference it all works just fine)

XML Declarations


<Style x:Key="enumEditorStyle" TargetType="{x:Type PropGrid:EnumEditorEx}"
            BasedOn="{StaticResource {x:Type actEditors:EnumEditBox}}">
    </Style>
    
    <DataTemplate x:Key="enumEditor">
        <PropGrid:EnumEditorEx
            Style="{StaticResource enumEditorStyle}"
            BorderThickness="0"
            Margin="5,0,0,0"
            VerticalAlignment="Center"
            g:InputExtender.OnEnterPressed="PropGrid:EnumEditorEx.Value"   
            Validation.ErrorTemplate="{DynamicResource {x:Static gr:ControlTemplates.RichToolTipValidationKey}}" 
            Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type actPrimitives:PropertyGridDataAccessorItem}}, Path=Value,
            UpdateSourceTrigger=PropertyChanged, ValidatesOnExceptions=True, ValidatesOnDataErrors=True}"
            IsEnabled="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type actPrimitives:PropertyGridDataAccessorItem}}, Path=IsReadOnly, Converter={StaticResource booleanNotConverter}}"
            />
    </DataTemplate>

Code of derived control

    public class EnumEditorEx : EnumEditBox
    {
        public EnumEditorEx()
        {
            PreviewKeyDown += new KeyEventHandler(EnumEditorEx_PreviewKeyDown);
            GotFocus += new RoutedEventHandler(EnumEditorEx_GotFocus);
            PreviewMouseDown += new MouseButtonEventHandler(EnumEditorEx_PreviewMouseDown);
        }

        void EnumEditorEx_PreviewMouseDown(object sender, MouseButtonEventArgs e)
        {
            Debug.WriteLine("enumEdit:" + e.OriginalSource.GetType().Name);
            EnumListBoxItem item = VisualTreeHelperHelper.FindVisualParentIncludingSelf<EnumListBoxItem>(e.OriginalSource);
            if (item != null)
            {
                Value = (Enum)item.DataContext;
                e.Handled = true;
            }
        }

        void EnumEditorEx_GotFocus(object sender, System.Windows.RoutedEventArgs e)
        {
            SelectFirstGroup();
        }

        void EnumEditorEx_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Add)
            {
                changeIndex(1);
                e.Handled = true;
            }
            if (e.Key == Key.Subtract)
            {
                changeIndex(-1);
                e.Handled = true;
            }
        }

        void changeIndex(int factor)
        {
            List<String> list = new List<String>(Enum.GetNames(Value.GetType()));
            for (int i = 0; i < list.Count; i++)
            {
                if (list[i] == Value.ToString())
                {
                    i += factor;
                    if (i > (list.Count - 1))
                        i = 0;

                    if (i < 0)
                        i = list.Count - 1;

                    Value = (Enum)Enum.Parse(Value.GetType(), list[i]);
                    SelectFirstGroup();
                    break;
                }
            }

        }
    }

Comments (1)

Posted 8 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hi Jeff,

The issue is your GotFocus event handler. That event is fired even for the drop-down content, as it's a bubbling event. So in effect, you are moving the focus back to the edit box when you try to click in the drop-down content.

You may need to ignore the even when the original source is an EnumListBoxItem.


Actipro Software Support

The latest build of this product (v2019.1 build 0683) 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.