Search code examples
c#wpfbuttonexpander

C# WPF Expander with multiple functioning buttons


I've got an expander (code below), to which I've added a StackPanel and a few buttons. They all show when I test the code, however I can't figure out how to code a click event for the buttons. Any ideas on how to get this functioning?

<Expander x:Name="DBListing" ExpandDirection="Down" Header="DB List" Background="#FF644117" Foreground="LightGoldenrodYellow">
    <StackPanel Orientation="Vertical">
        <Button x:Name="EdiBtn" Content="EDI DB" Margin="0,0,0,0" Width="100" Height="30" Background="LightGoldenrodYellow" Foreground="#FF644117"/>
        <Button x:Name="IssuesBtn" Content="Issues DB" Margin="0,10,0,0" Width="100" Height="30" Background="LightGoldenrodYellow" Foreground="#FF644117"/>
        <Button x:Name="InterimBtn" Content="Interim DB" Margin="0,10,0,0" Width="100" Height="30" Background="LightGoldenrodYellow" Foreground="#FF644117"/>
    </StackPanel>
</Expander>

I've tried setting up a click event for each button separately in the xaml.cs file, and I've also tried to set up a switch statement for the Expander. No luck thus far.


Solution

  • Subscribe to the Click event in the Xaml by these steps.

    1. In the button where you want the operation to be associated type Click=. Once done Visual Studio intellesense will offer a name as a popup. Click on that to accept. Notice how the attribute is now filled out like Click="ButtonOperation". In the future put you can put a name more fitting for the operation.
    2. Place the mouse cursor anywhere within the "{name}" text just created.
    3. Push the F12 key and you will be taken to code behind where the named method will be installed for you with the appropriate parameters.
    4. To test put in MessageBox.Show("Jabberwocky"); in the method, compile and run
    5. When you click on the button note that the messagebox pops up with "Jabberwocky".