简体   繁体   English

使用相对源和依赖属性进行绑定 - MVVM

[英]Binding using a relative source and dependency property - MVVM

Can someone explain me the Relativesource- ancestor feature in binding with MVVM. 有人可以解释我与MVVM绑定时的Relativesource-祖先功能。 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: 编辑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 CanEdit也是一个依赖属性

It searches through the elements Ancestors untill it finds an element of the AncestorType. 它搜索Ancestors元素,直到它找到AncestorType的元素。 This element will be taken as Source. 该元素将被视为Source。

In the following example the Button will have the same width as the Grid: 在以下示例中,Button的宽度与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. 此外,您可以通过设置AncestorLevel来设置要搜索的级别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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