2012-10-25 86 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