简体   繁体   中英

WPF designer issue referencing enum

So I have referenced an enum in this example using the staticextension, which works fine at runtime, but fails at design time due to the error "Could not create an instance of type 'StaticExtension'."

I understand this to mean that it thinks it needs an instance of the enum type to actually reference it. However, the enum is defined in the window as static so I don't understand why it is having issues.

Is there any reasonable way to keep the designer working? The closest I have found so far is to put it in an objectdataprovider and create methods to return the enum values. Behind the scenes this is basically creating an object to reference the static type and seems like too much work to just pull the enum values out. The goal here is just to be able to reference individual enum types and display them.

<Window 
  x:Class="DaedalusGraphViewer.GraphViewerWindow"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:DaedalusGraphViewer="clr-namespace:DaedalusGraphViewer">
 <StackPanel>
    <Label Content="testtesttest">
      <Label.ContextMenu>
        <ContextMenu>
          <MenuItem Header="{x:Static DaedalusGraphViewer:GraphViewerWindow+Test.test1}"/>
        </ContextMenu>
      </Label.ContextMenu>
    </Label>
  </StackPanel>  
</Window>

C#:

using System.Windows;

namespace DaedalusGraphViewer
{
  /// <summary>
  /// Interaction logic for GraphViewerWindow.xaml
  /// </summary>
  public partial class GraphViewerWindow : Window
  {
    public GraphViewerWindow()
    {
      InitializeComponent();
      Application.Current.MainWindow = this;
    }

    public enum Test
    {
      test1, test2
    }
  }
}

This is a known bug in designer. The workaround is not to use nested types with designer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM