Commands

StyleMVVM provides the class DelegateCommand which is a default implementation of the ICommand interface. Below is an example of how to wire up a button handler.

XAML:
    <Button Command="{Binding ClickCommand}">Click Me</Button>

ViewModel:
    private DelegateCommand clickCommand;

    public DelegateCommand ClickCommand
    { 
       get 
       {
           if(clickCommand == null)
           {
               clickCommand = new DelegateCommand(clickHandler);
           }

           return clickCommand;
       }
    }

    private void clickHandler(object parameter)
    {
    
    }  

Events

While Commands are nice you will need to handle more events than just Click. To that end StyleMVVM offers a simple way to use public methods from your ViewModel to handle events. Below is an example of how to hook up the ItemClick and SelectionChanged events on a GridView control.

XAML - short hand
    <GridView IsItemClickEnabled="True"
         View:EventHandlers.Attach="ItemClick => ItemClickHandler($sender,$eventArgs); SelectionChanged => SelectionChangedHandler($dataContext);" />

XAML - long hand
    <GridView IsItemClickEnabled="True">
        <View:EventHandlers.List>
           <View:EventHandlerList>
                  <View:EventHandlerInstance Attach="ItemClick => ItemClickHandler($sender,$eventArgs);"/>
                  <View:EventHandlerInstance Attach="SelectionChanged => SelectionChangedHandler($dataContext);"/>
           </View:EventHandlerList>
        </View:EventHandlers.List>
    </GridView>

ViewModel
    public void ItemClickHandler(FrameworkElement sender, ItemClickEventArgs clickEventArgs)
    {

    }

    public void SelectionChangedHandler(object dataContext)
    {

    }

Last edited Oct 16, 2013 at 8:45 PM by ipjohnson123, version 6

Comments

No comments yet.