Given the DataTemplate in which the button is defined:

<DataTemplate DataType="{x:Type localVM:SwitchISViewModel}"> <Grid Background="..."}" > <Button Margin="5" Content="{Binding Name}" Command="{Binding ExecuteComand}" /> </Grid> </DataTemplate> 

Further in Grid I use this Template

 <Grid> <StackPanel> <ContentControl Margin="4" Content="{Binding SwitchIS[0]}" > </ContentControl> <ContentControl Margin="4" Content="{Binding SwitchIS[1]}" > </ContentControl> </StackPanel> </Grid> 

where SwitchIS is the SwitchISViewModel array defined in MainViewModel. The fact is that everything is attached. The first time I click on a Button, I get into the ExecuteComand. Then it does not click. What can be wrong.

ps Tried to do through ContentPresenter: Defined

 <DataTemplate x:Key="TemplateVM"> .... 

Instead of ContentControl pasted ContentPresenter

 <ContentPresenter Content="{Binding SwitchIS[0]}" ContentTemplate="{StaticResource TemplateVM}" /> 

The result is the same.

  • Is it possible to write MCVE? - user227049
  • corrected, so better? - Konstantin Mefodiev
  • 2
    Without SwitchISViewModel, the question is not complete - Pavel Mayorov
  • Yes, I agree. Without SwitchISViewModel is not complete. But I figured it out. My MainViewModel contained the SwitchISViewModel. Prior to this, all RelayCommand were defined in the MainViewModel. Then I decided to simplify due to duplicate blocks and created SwitchISViewModel, where the RelayCommand was transferred. The result was a partial failure of work. The problem was solved by setting the keepTargetAlive = true flag in RelayCommand. Can anyone give an explanation of strong and weak links? or poke where to read? - Konstantin Mefodiev

0