Debug ElementName binding
Is there a way to futher debug bindings other than using PresentationTraceSources.TraceLevel?
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: querie开发者_开发知识库d 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')
Changes without any effect:
- Binding to the parent element of UserControl2 instead of UserControl2.
Non-failing scenarios (here the ElementName-binding works):
- UserControl2 is not placed within UserControl1.
- Button is added as a child to UserControl1 instead of UserControl2.
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?
Update
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 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.
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 probably forces element to be created, and thats why it appears.
精彩评论