开发者

Silverlight Model, View - ViewModel Troubles

开发者 https://www.devze.com 2023-02-23 01:28 出处:网络
Hi I\'m trying to make a Silverlight page with an event in the ViewModel but I don\'t understand how to do this on the page load event (I can\'t find the proper command).

Hi I'm trying to make a Silverlight page with an event in the ViewModel but I don't understand how to do this on the page load event (I can't find the proper command). I'd like to bind this: Loaded="RadPane_Loaded" to Loaded={Binding RadPane_Loaded}.

View:

namespace SilverlightTest.Modules.Tree
{
    public partial class OutlookBarView : RadPane
    {
        public OutlookBarView(OutlookBarViewModel model)
        {
            InitializeComponent();
            DataContext = model;
        }
    }
}

ViewModel:

namespace SilverlightTest.Modules.Tree
{
    public class OutlookBarViewModel : DependencyObject
    {
        private IEventAggregator _eventAggregator;
        private IMainPa开发者_如何学JAVAge _shell;
        private IUnityContainer _container;

        public OutlookBarViewModel(IEventAggregator eventAggregator, IMainPage shell, IUnityContainer container)
        {
            _container = container;
            _eventAggregator = eventAggregator;
            _shell = shell;

        }


        This is what I would normally do to bind something to a control.

public ICommand ExampleCommand
        {
            get { return (ICommand)GetValue(ExampleCommandProperty); }
            set { SetValue(ExampleProperty, value); }
        }

        /* Here I'd like to bind the page load event but I don't understand how...? */


     }
}


  1. Add to your project assemblies Microsoft.Expression.Interactions and System.Windows.Interativity from Blend SDK (if you use Prism these assemblies are included).
  2. Add command to view model, f.i. InitializeCommand
  3. And in XAML:

    <RadPane>
      <i:Interaction.EventTriggers>
        <i:EventTrigger EventName="Loaded">
            <i:InvokeCommandAction Command={Binding InitializeCommand}/>
        </i:EventTrigger>
      </i:Interaction.EventTriggers>
    </RadPane>
    

So, your viewmodel's command InitializeCommand will be invoked when Loaded event raises.


I found out that there is a very simple manner to send the EventArgs to the ViewModel with the Caliburn library. (http://caliburnmicro.codeplex.com/)

xmlns:caliburn="clr-namespace:Caliburn.Micro;assembly=Caliburn.Micro" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

            <i:Interaction.Triggers>
                <i:EventTrigger EventName="Loaded">
                    <i:InvokeCommandAction Command="{Binding GridViewLoaded}"/>
                </i:EventTrigger>
                <i:EventTrigger EventName="SelectionChanged">
                    <caliburn:ActionMessage MethodName="GridViewSelectionChangedCommandExecute">
                        <caliburn:Parameter Value="$eventArgs"></caliburn:Parameter>
                    </caliburn:ActionMessage>
                </i:EventTrigger>
            </i:Interaction.Triggers>

ViewModel:

public void GridViewSelectionChangedCommandExecute(SelectionChangeEventArgs e)
{  }

I'm wondering however whether the viewmodel knows too much now about the view.

0

精彩评论

暂无评论...
验证码 换一张
取 消