how to set focus programatically to a specific property item in xceed propertygrid

1

The closest solution posted was for a Mindscape propertygrid, so does not exactly translate. http://www.mindscapehq.com/forums/thread/1222 It uses an attached property. I am stuck on how to implement the attached property. I get this error in the XAML designer and when I try to run the app.

System.Windows.Markup.XamlParseException
  HResult=0x80131501
  Message=The type initializer for 'Restsys.Focuser' threw an exception.
  Source=PresentationFramework
  StackTrace:
   at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
   at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
   at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
   at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
   at Restsys.MainWindow.InitializeComponent() in C:\projects\Restsys\RestsysUI\MainWindow.xaml:line 1

Inner Exception 1:
ArgumentException: Default value type does not match type of property 'FocusId'.

Here is my attached property:

public class Focuser 
{
    public static readonly DependencyProperty FocusId = DependencyProperty
        .RegisterAttached("FocusId",
            typeof(string),
            typeof(Focuser),
            new FrameworkPropertyMetadata(false,
                FrameworkPropertyMetadataOptions.AffectsRender)
            );

    public static void SetFocusId(UIElement element, string value)
    {
        element.SetValue(FocusId, value);
    }

    public static string GetFocusId(UIElement element)
    {
        return (string)element.GetValue(FocusId);
    }

    public static void TrySetFocus(UIElement within, string focusId)
    {
        if (GetFocusId(within) == focusId)
        {
            ((FrameworkElement)within).Focus();
        }

        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(within); ++i)
        {
            TrySetFocus((FrameworkElement)VisualTreeHelper.GetChild(within, i), focusId);
        }
    }
}

And the XAML:

<xctk:PropertyGrid x:Name="EmployeeInfoPG" SelectedObject="{Binding SelectedEmployee}" ShowTitle="False" ShowSearchBox="False" ShowDescriptionByTooltip="False" ShowAdvancedOptions="False" ShowSummary="False" ShowSortOptions="False" AutoGenerateProperties="True" Width="357" SelectedProperty="{Binding FocusedEmployeeProperty, diag:PresentationTraceSources.TraceLevel=High }">
  <xctk:PropertyGrid.EditorDefinitions>
    <xctk:EditorDefinition>
      <xctk:EditorDefinition.PropertiesDefinitions>
        <xctk:PropertyDefinition Name="BirthDateStr"/>
      </xctk:EditorDefinition.PropertiesDefinitions>
      <xctk:EditorDefinition.EditorTemplate>
        <DataTemplate>
          <TextBox Text="{Binding Value}">
            <TextBox.Style>
              <Style>
                <Setter Property="local:Focuser.FocusId" Value="BirthDateStr" />
                </Style>
              </TextBox.Style>
            </TextBox>
          </DataTemplate>
        </xctk:EditorDefinition.EditorTemplate>
      </xctk:EditorDefinition>
    </xctk:PropertyGrid.EditorDefinitions>
  </xctk:PropertyGrid>
wpf
focus
propertygrid
xceed
asked on Stack Overflow Jan 16, 2018 by RasThavis • edited Jan 16, 2018 by GeorgDangl

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0