1

I want to be able to pass an enum parameter upon Loaded=" " so I can easily identify the section that is loading without having to do string trickery on the name.

My Expander XAML:

<Expander Loaded="ExpanderLoaded" x:Name="Greeting_And_Opening_Expander" ExpandDirection="Down" IsExpanded="True" FontSize="14" FontWeight="Bold" Margin="5" BorderThickness="1" BorderBrush="#FF3E3D3D">

The Method I want it to call:

    private void ExpanderLoaded(object sender, RoutedEventArgs e, Sections section)
    {
        //Do stuff
    }

My Enum (It will be significantly larger, this is just a test run):

public enum Sections
{
    Default = 0,
    Opening = 1,
    Verify = 2
}

How do I go about passing the enum as a parameter upon Loading?

Douglas Gaskell
  • 6,562
  • 6
  • 55
  • 103

1 Answers1

1

I would do this using EventTrigger and InvokeCommand action, that way in your view model ElementLoaded (For lack of a better name) gets called and the appropriate Enumeration gets passed in.

<Expander>
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Loaded">
            <i:InvokeCommandAction Command="{Binding ElementLoaded}" 
                               CommandParameter="{x:Static local:Sections.Default}"/>
        </i:EventTrigger>
    </i:Interaction.Triggers>
</Expander>

In your ViewModel, you will have a property of type ICommand called ElementLoaded, then in your constructor you initialize it as so

ElementLoaded = new ActionCommand(ElementLoadedMethod);

and the ElementLoadedMethod can be as so

    private void ElementLoadedMethod(object section)
    {
        var sectionEnumVal =  (Sections)section;
    }

This should be all you have to do.

TYY
  • 2,622
  • 1
  • 11
  • 14
  • Thank you for the reply. Is there a specific assembly I need for that? Tried adding ' xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" ' but that does not seem to be correct. – Douglas Gaskell May 27 '15 at 20:38
  • You have to add the assembly as well. In VS 2013, if you go to Add Reference and under extensions you should find System.Windows.Interactivity – TYY May 27 '15 at 20:45
  • Thank you, I really feel like an idiot here. I have already added the assembly into my references and added a xmlns:i ="clr-namespace:Microsoft.Windows.Design.Interaction;assembly=Microsoft.Windows.Design.Interaction" reference to the XAML sheet for it after the one above did not work. I'm still unable to use Interaction, EventTrigger, InvokeCommandAction...etc – Douglas Gaskell May 27 '15 at 20:56
  • Was adding interaction vs interactivity. Got it. Thanks – Douglas Gaskell May 27 '15 at 21:02
  • I was just going to say that the namespace is really all you should need. The approach should work now I assume. – TYY May 27 '15 at 21:04
  • Yep, now I just need to figure out how to actually use it. Trying to read up on how to use the InvokeCommandAction since I'm probably not using it right (since my method is not actually called). The information seems a bit scattered for a newbie to WPF. – Douglas Gaskell May 27 '15 at 21:23