简体   繁体   中英

Binding using a relative source and dependency property - MVVM

Can someone explain me the Relativesource- ancestor feature in binding with MVVM. basically a dependency property is bound to a button with the above concept thru view models. It will be great for some explanation with the samples. Or else some links would also do.

Edit 1:

<Button Name="button1" Content ="Edit" Margin="0, 0, 5, 0" Style="{StaticResource button}"  
        Grid.Column="0" Visibility="{Binding cMode, Converter={StaticResource VisibilityConverter}, ConverterParameter={StaticResource invert}}"  
        Click="EditButton_Click"  
        IsEnabled="{Binding Path= Data.User.CanEdit, RelativeSource={RelativeSource FindAncestor, AncestorType=views:SCView, AncestorLevel=1}}"/>

Also CanEdit is a dependencyproperty

It searches through the elements Ancestors untill it finds an element of the AncestorType. This element will be taken as Source.

In the following example the Button will have the same width as the Grid:

<Grid Width="100"> <!--Level=2-->
    <Border> <!--Level=1-->
        <Button Width="{Binding RelativeSource={RelativeSource AncestorType={x:Type Grid}}, Path=ActualWidth}"/>
    </Border>
</Grid>

Additionally you can set the Level where to search by setting the AncestorLevel.

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