0
<DataTemplate DataType="{x:Type local:TestModel}">    
    <Button Content="Button" " Margin="0">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="Click">
                <mvvm:EventToCommand  
                    Command="{Binding ImageClick, Mode=OneWay}" 
                    MustToggleIsEnabledValue="True"/>
             </i:EventTrigger>
        </i:Interaction.Triggers>
    </Button>
</DataTemplate>

Hi all,

Above I have a data template which is switching on certain data type using a data template. I am using the mvvm pattern. I want to be to bind to the ImageClick property.

I was able to do this before, but since I have moved the button inside the data template I am unable to bind to that property.

I think I need to use Relative source for the binding but am really unsure how to do it.

Help on this would be amazing.

Thanks.

Phil
  • 42,255
  • 9
  • 100
  • 100
user101010101
  • 1,609
  • 6
  • 31
  • 52
  • 2
    Maybe this answer will be helpful http://stackoverflow.com/questions/10596094/bind-command-from-business-object-to-view-in-mvvm/10596243#10596243 – Zabavsky Jun 07 '12 at 13:19
  • It looks ok, depending on where the ImageClick command is. Can we see the view model? – Phil Jun 07 '12 at 17:07
  • Does this answer your question? [Binding to viewmodel from inside a datatemplate](https://stackoverflow.com/questions/18245936/binding-to-viewmodel-from-inside-a-datatemplate) – Vitali Dzikavitski Dec 14 '22 at 12:26

1 Answers1

4

You may try the below.

<Button Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type YourViewModel}}, Path=DataContext.ImageClick}" />

I think it would help you..

David Bekham
  • 2,175
  • 3
  • 27
  • 56
  • 3
    The answer is quite right except type: x:Type YourViewModel shall be changed into x:Type YourView – Serge P Aug 19 '15 at 10:14