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

时间:2013-02-22 09:39:38

标签: c# wpf data-binding mvvm viewmodel

有人可以解释我与MVVM绑定时的Relativesource-祖先功能。基本上,依赖属性通过视图模型绑定到具有上述概念的按钮。对样本进行一些解释会很棒。或者也可以使用一些链接。

编辑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}}"/>

CanEdit也是一个依赖属性

1 个答案:

答案 0 :(得分:2)

它搜索Ancestors元素,直到它找到AncestorType的元素。该元素将被视为来源。

在以下示例中,Button的宽度与Grid相同:

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

此外,您可以通过设置AncestorLevel来设置要搜索的等级。