【发布时间】:2011-06-13 09:00:31
【问题描述】:
我正在尝试创建一个复杂的工具提示,其中一个 TextBlock 将绑定到 UserControl 的属性,在该属性中,此工具提示被定义为资源。 XAML 代码的简化版本如下所示:
<UserControl x:Class="WpfApplication3.TestPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
xmlns:pixellab="clr-namespace:PixelLab.Wpf;assembly=UIControls"
xmlns:my="clr-namespace:WpfApplication3"
d:DesignHeight="499" d:DesignWidth="409"
x:Name="PageInstance" >
<UserControl.Resources>
<Grid x:Key="Tooltip">
<TextBlock Text="{Binding ElementName=PageInstance, Path=PageTest}"/>
</Grid>
</UserControl.Resources>
<Border Background="Red" ToolTip="{StaticResource Tooltip}" />
UserControl 被命名为 PageInstance。显示在里面的边框有一个定义为资源的工具提示。如果我尝试将文本与
绑定<TextBlock Text="{Binding ElementName=PageInstance, Path=PageTest}"/>
我在运行应用程序时遇到绑定错误:
System.Windows.Data 错误:4:找不到与引用“ElementName=PageInstance”进行绑定的源。绑定表达式:路径=页面测试;数据项=空;目标元素是'TextBlock'(名称='');目标属性是“文本”(类型“字符串”)
如何才能成功地将文本绑定到 PageInstance 用户控件中的 PageTest 属性?
感谢您的帮助。我还没有完全弄清楚绑定是如何工作的。 格雷戈尔
【问题讨论】:
标签: wpf data-binding binding resources