【问题标题】:WPF Binding RelativeSourceWPF绑定RelativeSource
【发布时间】:2012-10-25 21:37:53
【问题描述】:

这里只是做一个小测试。只有这两个文本框中的第一个显示值“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>

【问题讨论】:

    标签: c# wpf binding relativesource


    【解决方案1】:

    RelativeSource代替Source,像这样:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-28
      • 1970-01-01
      • 1970-01-01
      • 2013-09-01
      • 1970-01-01
      • 2012-02-06
      相关资源
      最近更新 更多