2013-02-22 50 views
0

有人可以向我解釋綁定MVVM時的Relativesource-ancestor功能。基本上依賴屬性通過視圖模型綁定到具有上述概念的按鈕。這將是很好的一些樣品的解釋。否則一些鏈接也會這樣做。使用相對來源和依賴屬性進行綁定 - 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是一個DependencyProperty

+0

您是否閱讀過[文章](http://msdn.microsoft.com/en-us/library/ms743599.aspx)? – DHN 2013-02-22 09:48:55

+0

我已閱讀,但我不是很清楚... – user1687824 2013-02-22 10:00:08

+0

什麼是你的'DataContext'?從我看到的我認爲,你不需要'RelativeSource'綁定。 – DHN 2013-02-22 10:06:40

回答

2

它搜索通過元件祖先,直到它找到AncestorType的元件。這個元素將被視爲來源。

在下面的例子中,按鈕將具有相同寬度的網格:

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

此外,您可以設置水平,通過設置AncestorLevel進行搜索。

+0

請檢查我的edit1。那是不是祖先是SCView的viewmodel? – user1687824 2013-02-22 09:59:37

+0

試試這個:'Path = DataContext.Data.User.CanEdit' – 2013-02-22 10:12:04

+0

嗯......我可以看到一些區別。這種變化意味着什麼?我的意思是我沒有,不知道這個變化會發生什麼 – user1687824 2013-02-22 10:26:30