简体   繁体   中英

WP8 Listbox Loaded MVVM

I'm making a WP8 application using the MVVM patern (Caliburn.Micro).

I'm using a ListBox Named ProgramsList and I want to do something when Loaded.

<ListBox Name="ProgamsList" ItemsSource="{Binding ProgramsList}" HorizontalAlignment="Stretch" FontFamily="Portable User Interface" Loaded="">

When not using the MVVM patern I could use the automatic generated Event handler.

How can I do this the right way using the MVVM patern?

    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
    xmlns:cmd="clr-namespace:GalaSoft.MvvmLight.Command;assembly=GalaSoft.MvvmLight.Extras"

<ListBox Name="ProgamsList" ItemsSource="{Binding ProgramsList}" HorizontalAlignment="Stretch" FontFamily="Portable User Interface" >
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Loaded">
            <cmd:EventToCommand Command="{Binding LoadedCommand}" />
        </i:EventTrigger>        
    </i:Interaction.Triggers>
</ListBox>


    public RelayCommand LoadedCommand
            {
                get;
                private set;
            }

            /// <summary>
            /// Initializes a new instance of the SplashScreenViewModel class.
            /// </summary>
            public SplashScreenViewModel()
            {
                LoadedCommand = new RelayCommand(toDoSomehing);
            }

    private void toDoSomething(){
    }

You can use commands to expose your logic from the ViewModel an then use behaviors, for example: http://metroeventtocommand.codeplex.com/

If that doesn't fit your needs, you can always use an event handler and call the command from there.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM