
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 DeclarationsCode of derived control
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>
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;
}
}
}
}