调试ElementName绑定 [英] Debug ElementName binding

查看:172
本文介绍了调试ElementName绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法进一步调试绑定,而不是使用PresentationTraceSources.TraceLevel?

Is there a way to futher debug bindings other than using PresentationTraceSources.TraceLevel?

我有一个相当复杂的问题,其中ElementName绑定失败(元素是当在UserControl2上放置在UserControl1中的作为小孩的Button上的CommandParameter上使用该对象时,未找到它。 PresentationTraceSources.TraceLevel = High的输出是(PåstandsListe是UserControl2):

I have a rather complex issue, where an ElementName-binding fails (the element is not found) when it is used on a CommandParameter on a Button that is a child on UserControl2 which is placed inside UserControl1. The output of PresentationTraceSources.TraceLevel=High is (PåstandsListe is UserControl2):

System.Windows.Data Warning: 52 : Created BindingExpression (hash=8957325) for Binding (hash=61342683)
System.Windows.Data Warning: 54 :   Path: 'SelectedItem'
System.Windows.Data Warning: 56 : BindingExpression (hash=8957325): Default mode resolved to OneWay
System.Windows.Data Warning: 57 : BindingExpression (hash=8957325): Default update trigger resolved to PropertyChanged
System.Windows.Data Warning: 58 : BindingExpression (hash=8957325): Attach to System.Windows.Controls.Button.CommandParameter (hash=426476)
System.Windows.Data Warning: 63 : BindingExpression (hash=8957325): Resolving source 
System.Windows.Data Warning: 66 : BindingExpression (hash=8957325): Found data context element: <null> (OK)
System.Windows.Data Warning: 70 :     Lookup name PåstandsListe:  queried Button (hash=426476)
System.Windows.Data Warning: 61 : BindingExpression (hash=8957325): Resolve source deferred
'Domstolene.JFS.GUI.vshost.exe' (Managed (v2.0.50727)): Loaded 'C:\Windows\assembly\GAC_64\System.Transactions\2.0.0.0__b77a5c561934e089\System.Transactions.dll', Symbols loaded.
System.Windows.Data Warning: 63 : BindingExpression (hash=8957325): Resolving source 
System.Windows.Data Warning: 66 : BindingExpression (hash=8957325): Found data context element: <null> (OK)
System.Windows.Data Warning: 70 :     Lookup name PåstandsListe:  queried Button (hash=426476)
System.Windows.Data Warning: 63 : BindingExpression (hash=8957325): Resolving source 
System.Windows.Data Warning: 66 : BindingExpression (hash=8957325): Found data context element: <null> (OK)
System.Windows.Data Warning: 70 :     Lookup name PåstandsListe:  queried Button (hash=426476)
System.Windows.Data Warning: 63 : BindingExpression (hash=8957325): Resolving source 
System.Windows.Data Warning: 66 : BindingExpression (hash=8957325): Found data context element: <null> (OK)
System.Windows.Data Warning: 70 :     Lookup name PåstandsListe:  queried Button (hash=426476)
System.Windows.Data Warning: 63 : BindingExpression (hash=8957325): Resolving source 
System.Windows.Data Warning: 66 : BindingExpression (hash=8957325): Found data context element: <null> (OK)
System.Windows.Data Warning: 70 :     Lookup name PåstandsListe:  queried Button (hash=426476)
System.Windows.Data Warning: 63 : BindingExpression (hash=8957325): Resolving source 
System.Windows.Data Warning: 66 : BindingExpression (hash=8957325): Found data context element: <null> (OK)
System.Windows.Data Warning: 70 :     Lookup name PåstandsListe:  queried Button (hash=426476)
System.Windows.Data Warning: 63 : BindingExpression (hash=8957325): Resolving source 
System.Windows.Data Warning: 66 : BindingExpression (hash=8957325): Found data context element: <null> (OK)
System.Windows.Data Warning: 70 :     Lookup name PåstandsListe:  queried Button (hash=426476)
System.Windows.Data Warning: 63 : BindingExpression (hash=8957325): Resolving source  (last chance)
System.Windows.Data Warning: 66 : BindingExpression (hash=8957325): Found data context element: <null> (OK)
System.Windows.Data Warning: 70 :     Lookup name PåstandsListe:  queried Button (hash=426476)
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=PåstandsListe'. BindingExpression:Path=SelectedItem; DataItem=null; target element is 'Button' (Name=''); target property is 'CommandParameter' (type 'Object')

更改无任何影响:


  • 绑定到
    的主元素UserControl2而不是UserControl2。

无故障的场景(此处为ElementName绑定):

Non-failing scenarios (here the ElementName-binding works):


  • UserControl2未放置在UserControl1中。 li>
  • 按钮作为一个小孩添加到UserControl1而不是UserControl2。

此外,当我附加Snoop当我在Snoop中选择Button时(但没有信息显示在VS Output窗口中),ElementName绑定的应用程序开始工作。 Snoop以某种方式刷新绑定?

Futhermore when I attach Snoop to the application the ElementName-binding starts working when I select the Button within Snoop (but no information is displayed in the VS Output window). Does Snoop somehow refresh the binding?

似乎问题只发生在Button最初没有可见的,例如放置在不可见的Expander og TabItem中。

It seems the problem only occurs when the Button is not initially visible, e.g. placed in an Expander og TabItem which are not visible.

推荐答案

Tabs和Expander控件通常不会创建和初始化项目,除非它们是可见,只有当它们第一次被显示时,这些项目被创建和激活,这就是为什么第一次绑定可能失败。当没有创建元素时,没有这样的解决方案,因为它不会出现在视觉树中。

Tabs and Expander controls usually do not create and initialize item unless they are visible, only when first time they are made visible, the items are created and initalized thats why first time the binding may fail. There is no such solution for this case when element is not created as it does not appear in visual tree.

原因很简单,如果你有一个模板控件,控件的模板不会创建,直到控件可见并添加到视觉树中。由于这个原因,模板中的项目不存在,无法绑定,所以Tabs和Expander的子级别不会与父级一起创建。

The reason is pretty simple, if you have a templated control, the template of control is not created till the control is visible and added in visual tree. Because of this reason, the items inside template do not exist and can not be bound to, now Tabs and Expander's child etc are not created along with the parent.

Snoop可能force元素被创建,这就是为什么它出现。

Snoop probably forces element to be created, and thats why it appears.

这篇关于调试ElementName绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆