views:

30

answers:

2

In Expression Blend 4 (Silverlight project) I have a UserControl to which I have added a CLR property. This property is an enum type, which is defined within the UC. I have attached a ChangePropertyAction behaviour to an instance of the UC. However, the XAML parser gives the following error (among others):

'+' is not valid in a name

This is because the following XAML (snippet) has been generated:

<local:SomeControl Margin="155,113,317,0" d:LayoutOverrides="Width, Height">
  <i:Interaction.Triggers>
    <i:EventTrigger EventName="MouseLeftButtonDown">
      <ei:ChangePropertyAction PropertyName="MyProp">
        <ei:ChangePropertyAction.Value>
          <local:SomeControl+MyEnum>Second</local:SomeControl+MyEnum> <----- Error on this line caused by the '+' 
        </ei:ChangePropertyAction.Value>
      </ei:ChangePropertyAction>
    </i:EventTrigger>
  </i:Interaction.Triggers>
</local:SomeControl>

The code behind:

public partial class SomeControl : UserControl
{
    public SomeControl()
    {
        // Required to initialize variables
        InitializeComponent();
    }

    public MyEnum MyProp
    {
        get; set;
    }

    public enum MyEnum
    {
        First,
        Second,
        Third
    }
}

A simple workround is to "promote" the enum out from within the class (eg SomeControl_MyEnum), but is there a cleaner solution?

+1  A: 

You need to make use of the x:Static markup extension, don;t forget to add namespace in XAML as needed.

Sample would be:

"{x:Static Member=MyProject:MyEnum.First}"

If you want to bring in binding into the equation, read this

Aaron
Thanks for your response. However it turns out that x:Static is WPF only and I failed to mention that my project is Silverlight. Sorry, stupid of me.
SoundCrowd
After some more trial and error I've found that I can simply replace the ChildPropertyAction.Value element and insert the required enum value as the Value attribute of the parent element. Of course every time I alter the ChangePropertyAction behaviour in Blend I have to go back in and edit again.
SoundCrowd
Ahh, I see I **did** mention Silverlight in the tags, but not the main body. Have updated post to clarify.
SoundCrowd
A: 

Using a nested type name in Xaml is not supported. You can still specify the value of the property without referring to the type name. Either of the following should work:

<local:SomeControl Margin="155,113,317,0" d:LayoutOverrides="Width, Height">
  <i:Interaction.Triggers>
    <i:EventTrigger EventName="MouseLeftButtonDown">
      <ei:ChangePropertyAction PropertyName="MyProp">
        <ei:ChangePropertyAction.Value>Second</ei:ChangePropertyAction.Value>
      </ei:ChangePropertyAction>
    </i:EventTrigger>
  </i:Interaction.Triggers>
</local:SomeControl>

or

<local:SomeControl Margin="155,113,317,0" d:LayoutOverrides="Width, Height">
  <i:Interaction.Triggers>
    <i:EventTrigger EventName="MouseLeftButtonDown">
      <ei:ChangePropertyAction PropertyName="MyProp" Value="Second" />
    </i:EventTrigger>
  </i:Interaction.Triggers>
</local:SomeControl>

If it is important to you to be able to refer to the MyEnum type from Xaml, you will need to move the definition out of the SomeControl class.

KeithMahoney