2012-10-25 112 views
1

只是在這裏做一個小測試。只有這兩個文本框中的第一個顯示值「123」。爲什麼不是第二個?WPF綁定RelativeSource

<Window x:Class="Test.MainWindow" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase" 
    Title="MainWindow" Height="350" Width="525"> 
<StackPanel Width="123" x:Name="Panel1"> 
    <TextBox Text="{Binding ElementName=Panel1, Path=Width, diag:PresentationTraceSources.TraceLevel=High}"></TextBox> 
    <TextBox Text="{Binding Source={RelativeSource AncestorType={x:Type StackPanel}}, Path=Width, diag:PresentationTraceSources.TraceLevel=High}"></TextBox> 
</StackPanel> 

回答

5

而不是Source採取RelativeSource,就像這樣:

<TextBox Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type StackPanel}}, Path=Width, diag:PresentationTraceSources.TraceLevel=High}"></TextBox> 
+0

尼斯一個,謝謝! – MrNick