我在我的WPF应用程序中使用DataContextProxy有麻烦。 当我把一个DataContextProxy在网格中的参考资料它永远不会加载。 如果我移动DataContextProxy了资源节的一切工作正常。
我一直在研究这一段时间,尝试了许多方法来调试应用程序。
我放置在我试图使用与代理的控制DebugConverter。 调试器永远不会被调用。
我用WPFSnoop,看看是否有任何约束力的错误。 我得到以下绑定错误的DataContextProxy,
System.Windows.Data错误:3:找不到元素提供的DataContext。 BindingExpression:(无路径); 的DataItem = NULL; 目标元件是“代理”(名称=“”); 目标属性是“的DataContext”(类型为“Object”)
我已经放在我的DataContextProxy的负载情况下一个断点。 Loaded事件不会被调用,我已经放在一个断点在DataContextChanged仅事件,永远不会被调用。
下面是一些示例代码来证明这一点。 很显然,我知道我并不真的需要在文本框使用DataContextProxy。
<Window x:Class="WpfDataContextProxyBug.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfDataContextProxyBug"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<local:DebugConverter x:Key="DebugConverter"/>
</Window.Resources>
<Grid>
<Grid.Resources>
<local:Proxy x:Key="Proxy" DataContext="{Binding}" />
</Grid.Resources>
<TextBox DataContext="{Binding Path=Name, Source={StaticResource Proxy}, Converter={StaticResource DebugConverter}}"/>
</Grid>
</Window>
该DataContextProxy类
public class Proxy : FrameworkElement
{
public Proxy()
{
Loaded += DataContextProxy_Loaded;
DataContextChanged += Proxy_DataContextChanged;
}
void Proxy_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
{
}
void DataContextProxy_Loaded(object sender, RoutedEventArgs e)
{
}
}