문제

Just doing a little test here. Only the first of these two text boxes displays the value "123". Why doesn't the second?

<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>

도움이 되었습니까?

해결책

Instead of Source take RelativeSource, like so:

<TextBox Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type StackPanel}}, Path=Width, diag:PresentationTraceSources.TraceLevel=High}"></TextBox>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top