WPF 绑定相对源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13077703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 05:57:08  来源:igfitidea点击:

WPF Binding RelativeSource

c#wpfbindingrelativesource

提问by MrNick

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

只是在这里做一个小测试。只有这两个文本框中的第一个显示值“123”。为什么没有第二个?

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

回答by Florian Gl

Instead of Sourcetake RelativeSource, like so:

而不是Sourcetake RelativeSource,像这样:

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