0

有人可以解释一下与 MVVM 绑定的相对源祖先特性吗?基本上,依赖属性通过视图模型绑定到具有上述概念的按钮。对样本进行一些解释会很棒。否则一些链接也可以。

编辑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 也是一个依赖属性

4

1 回答 1

2

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

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

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

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

于 2013-02-22T09:50:32.563 回答