1

I would like to bind any UI-Event directly to a command in my ViewModel. Is there a way to do that in UWP?

I'm using the UWP Community Toolkit and would like to bind the Hamburger-Menu-Item-Click to an command.

Thanks Tobias

1
  • yes, thank you! that is what I needed! Commented Apr 9, 2017 at 9:19

1 Answer 1

8

Ok I found the solution for that. You can find it here: Behaviors in UWP (Event trigger command error

I will repeat my steps here:

1 - Install Package

Install-Package Microsoft.Xaml.Behaviors.Uwp.Managed

2 - Include references to you view

xmlns:i="using:Microsoft.Xaml.Interactivity"
xmlns:core="using:Microsoft.Xaml.Interactions.Core"

3- Add the command to your ViewModel

        public DelegateCommand MenuItemCommand { get; set; }

        public void InitCommands()
        {
            MenuItemCommand = new DelegateCommand(MenuItemClicked);                            
        }

        private void MenuItemClicked()
        {
            //do something when clicked
        }

4 - Bind the Click-Event of the menuitem to the command in the view (see the i:...-tag)

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <controls:HamburgerMenu x:Name="HamburgerMenuControl"
                            PaneBackground="Black"
                            Foreground="White"
                            ItemTemplate="{StaticResource DefaultTemplate}"
                            OptionsItemTemplate="{StaticResource DefaultTemplate}"
                            ItemsSource="{Binding MenuItems}"
                            OptionsItemsSource="{Binding MenuOptionItems}"
                            >
        <i:Interaction.Behaviors>
            <core:EventTriggerBehavior EventName="ItemClick">
                <core:InvokeCommandAction Command="{Binding MenuItemCommand}"></core:InvokeCommandAction>
            </core:EventTriggerBehavior>
        </i:Interaction.Behaviors>
        <Frame x:Name="ContentFrame"/>
    </controls:HamburgerMenu>
</Grid>

when I know click the menu-item it

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.