简体   繁体   中英

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.

You may try the below.

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

I think it would help you..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM