简体   繁体   English

WPF中的动态XAML丢失了数据上下文

[英]Datacontext lost with dynamic xaml in WPF

I have a window with a simple frame element within it (we'll call it "myFrame"). 我有一个带有简单框架元素的窗口(我们称其为“ myFrame”)。 Within the Window.Loaded I am parsing a string variable (loaded from an external source) with XamlReader.Parse(string) and the result is a Page object (local var is called "myPage"). 在Window.Loaded中,我正在使用XamlReader.Parse(string)解析一个字符串变量(从外部源加载),结果是一个Page对象(本地var称为“ myPage”)。 I'm setting myFrame.Content = myPage. 我正在设置myFrame.Content = myPage。

Inside of the page is a label, like so: 页面内部是一个标签,如下所示:

<label Name="DataBindingTestLabel" Content="{Binding Path=TestLabel, TargetNullValue='Null value'}" />

I am trying to provide an anonymous type (for easy testing) as the frame's datacontext, like: 我正在尝试提供一个匿名类型(为便于测试)作为框架的数据上下文,例如:

this.ContentFrame.DataContext = new { TestLabel = "Hello, world." };

I have verified that the DataContext property has the value after it's set and keeps the value for as long as I can step through the window's execution, but the value is lost sometime before the window is rendered and the fallback value, "Null value," is displayed. 我已经验证了DataContext属性在设置后是否具有该值,并保持该值只要我可以逐步执行该窗口,但是该值在呈现窗口和后备值“ Null值”之前的某个时间就丢失了被陈列。

After enabling databinding tracing, the output window in visual studio shows: 启用数据绑定跟踪之后,Visual Studio中的输出窗口将显示:

System.Windows.Data Information: 41 : BindingExpression path error: 'TestLabel' property not found for 'object' because data item is null. System.Windows.Data信息:41:BindingExpression路径错误:找不到“对象”的“ TestLabel”属性,因为数据项为null。 This could happen because the data provider has not produced any data yet. 发生这种情况是因为数据提供者尚未产生任何数据。 BindingExpression:Path=TestLabel; BindingExpression:Path = TestLabel; DataItem=null; DataItem = null; target element is 'Label' (Name='DataBindingTestLabel'); 目标元素是“标签”(名称=“ DataBindingTestLabel”); target property is 'Content' (type 'Object') 目标属性为“内容”(类型为“对象”)

Any insight into something I'm doing wrong is most appreciated. 任何对我做错事的见解都将受到赞赏。

I hope someone else can confirm this, since I've only been using anonymous types myself very recently, but the MSDN article on anonymous types seems to indicate that, as I suspected, your anonymous type is simply going out of scope. 我希望其他人可以确认这一点,因为我最近才刚刚使用匿名类型,但是MSDN上有关匿名类型的文章似乎表明,正如我所怀疑的那样,您的匿名类型只是超出了范围。 In the Remarks section of the page linked above, it says "An anonymous type has method scope," so I'm inclined to believe that's your problem. 在上面链接的页面的“备注”部分中,它说“匿名类型具有方法范围”,因此我倾向于认为这是您的问题。

Again, I'm not entirely sure, just thought I'd offer my thoughts on the matter... 再说一次,我不确定,只是想就此事发表自己的看法...

As it turns out, the frame element has issues dealing with data context while the content is a page element. 事实证明,当内容是页面元素时,框架元素存在处理数据上下文的问题。

The solution was to use a more generic element (ContentControl) and instead of having the dynamic content be a Page element, we are using UserControl elements. 解决方案是使用更通用的元素(ContentControl),而不是将动态内容作为Page元素,而是使用UserControl元素。 In this setup, datacontext is not lost and binding works as expected. 在这种设置中,数据上下文不会丢失,并且绑定可以按预期进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM