user101010101
user101010101

Reputation: 1659

MVVM WPF bindings with RelativeSource

<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.

Upvotes: 0

Views: 5091

Answers (1)

David Bekham
David Bekham

Reputation: 2175

You may try the below.

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

I think it would help you..

Upvotes: 4

Related Questions