如何以编程方式将焦点设置为xceed propertygrid中的特定属性项

时间:2018-01-16 15:13:18

标签: wpf focus propertygrid xceed

发布的最接近的解决方案是针对Mindscape的属性网格,因此不能完全翻译。 http://www.mindscapehq.com/forums/thread/1222它使用附加属性。我坚持如何实现附加属性。我在XAML设计器中以及当我尝试运行应用程序时收到此错误。

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

这是我附属的财产:

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);
        }
    }
}

和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>

0 个答案:

没有答案