How do you submit LayoutRoot to RelayCommand via EventToCommand?

Example grid using a trigger:

<Grid x:Name="LayoutRoot" DataContext="{Binding ProjectGrid, Source={StaticResource Locator}}">
<i:Interaction.Triggers>
  <i:EventTrigger EventName="Loaded">
    <GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding LoadedCommand, Mode=OneWay}" PassEventArgsToCommand="True"/>
  </i:EventTrigger>
</i:Interaction.Triggers>

In my ViewModel model, I install LoadedCommand as follows:

public RelayCommand<RoutedEventArgs> LoadedCommand {get;private set;}

And in the ViewModel initializer, I have the following:

public ProjectGridViewModel()
{
  LoadedCommand = new RelayCommand<RoutedEventArgs>(e => 
    {
      this.DoLoaded(e);
    }
  );
}

Then in my DoLoaded, I try to do this:

Grid _projectGrid = null;
public void DoLoaded(RoutedEventArgs e)
{
  _projectGrid = e.OriginalSource as Grid;
}

You can see that I'm trying to get rid of my Loaded = "" in my Grid in my opinion and instead do RelayCommand. The problem is that the original source returns nothing. My loaded event works like this, but I need to get the Grid via RoutedEventArgs.

I tried passing to the Grid in EventCommand with CommandParameter = "{Binding ElementName = LayoutRoot}", but it just crashes VS2010 when I press F5 and start the project.

? ? Loaded, #, ViewModel Views, . ViewMode .

+3
1

CommandParameter EventToCommand:

<GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding LoadedCommand, Mode=OneWay}" CommandParameter="{Binding ElementName=LayoutRoot}" PassEventArgsToCommand="True"/>

:

public RelayCommand<UIElement> LoadedCommand {get;private set;} 

public ProjectGridViewModel() 
{ 
  LoadedCommand = new RelayCommand<UIElement>(e =>  
    { 
      this.DoLoaded(e); 
    } 
  ); 
} 

Grid _projectGrid = null; 
public void DoLoaded(UIElement e) 
{ 
  _projectGrid = e; 
} 

:)

Bye.

+4

Source: https://habr.com/ru/post/1746499/


All Articles