none
(MVVM): Call Method On ViewModel From View

    Question

  • Hey All,

     

    In my View, I would like to call a method on my ViewModel (all from XAML).

    View:

              <!--Parameters-->
              <ListView Grid.Row="2" 
                   DataContext="{Binding}"
                   ItemsSource="{Binding}">
                <ListView.View>
                  <GridView AllowsColumnReorder="True">
                    <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Key}"/>
                    <GridViewColumn Header="Value">
                      <GridViewColumn.CellTemplate>
                        <DataTemplate>
                          <TextBox Text="{Binding Value, Mode=OneWay}" MinWidth="100" />
                        </DataTemplate>
                      </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                  </GridView>
                </ListView.View>
              </ListView>
    

    ViewModel (the Method I want to call):

        public ReadOnlyScriptParameterCollection GetParameters(string scriptFullTypeName)
        {
          ReadOnlyScriptParameterCollection sps = null;
          var scriptHost = ScriptHost.Create<JdsScriptHost>();
          scriptHost.LoadFromAssembly(ScriptAssemblyPath, scriptFullTypeName);
          sps = scriptHost.ScriptParameters;
    
          return sps;
        }
    

    Is there a way to perhaps set the DataContext to this method's return value?

    Note that the method takes a parameter. I suppose this is where a converter would come in.

     

    So would it look something like??

    <ListView DataContext="{Binding Converter={StaticResource MyConverter}}"/>

    Thanks for any advice

    -catch

    Thursday, September 16, 2010 3:32 PM

Answers

  • You should be able to use ObjectDataProvider and specify the method for this. You can later use that for DataContext.

    http://msdn.microsoft.com/en-us/library/aa348824(v=VS.90).aspx

    Thursday, September 16, 2010 7:02 PM
  • Hi mariocatch15,

    You could bind a value to the MethodParameters property of the ObjectDataProvider  in XAML in "OneWayToSource" mode.

       <ObjectDataProvider x:Key="objectDataProvider">
        <ObjectDataProvider.MethodParameters>
         ...
        </ObjectDataProvider.MethodParameters>
       </ObjectDataProvider>
       ......
       <Binding Source="{StaticResource objectDataProvider}"  
                  Path="MethodParameters[0]"  
                  BindsDirectlyToSource="True"
                  Mode="OneWayToSource"/>
    

    to update the parameters at run-time.

    Sincerely,
    Bob Bao


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Are you looking for a typical code sample? Please download all in one code framework !
    Tuesday, September 21, 2010 10:59 AM
    Moderator

All replies

  • You should be able to use ObjectDataProvider and specify the method for this. You can later use that for DataContext.

    http://msdn.microsoft.com/en-us/library/aa348824(v=VS.90).aspx

    Thursday, September 16, 2010 7:02 PM
  • ObjectDataProvider in XAML doesn't support parameters at runtime (non-static parameters) unfortunately.

    Thursday, September 16, 2010 7:05 PM
  • Hi mariocatch15,

    You could bind a value to the MethodParameters property of the ObjectDataProvider  in XAML in "OneWayToSource" mode.

       <ObjectDataProvider x:Key="objectDataProvider">
        <ObjectDataProvider.MethodParameters>
         ...
        </ObjectDataProvider.MethodParameters>
       </ObjectDataProvider>
       ......
       <Binding Source="{StaticResource objectDataProvider}"  
                  Path="MethodParameters[0]"  
                  BindsDirectlyToSource="True"
                  Mode="OneWayToSource"/>
    

    to update the parameters at run-time.

    Sincerely,
    Bob Bao


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Are you looking for a typical code sample? Please download all in one code framework !
    Tuesday, September 21, 2010 10:59 AM
    Moderator