首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >WPF绑定RelativeSource

WPF绑定RelativeSource
EN

Stack Overflow用户
提问于 2012-10-26 05:37:54
回答 1查看 5.9K关注 0票数 1

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

代码语言:javascript
复制
<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>

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-10-26 05:40:43

使用RelativeSource代替Source,如下所示:

代码语言:javascript
复制
<TextBox Text="{Binding RelativeSource={RelativeSource AncestorType={x:Type StackPanel}}, Path=Width, diag:PresentationTraceSources.TraceLevel=High}"></TextBox>
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13077703

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档