前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >WPF 如何调试 binding

WPF 如何调试 binding

作者头像
林德熙
发布2018-09-18 17:16:34
1.2K0
发布2018-09-18 17:16:34
举报
文章被收录于专栏:林德熙的博客林德熙的博客

如果是写在 xaml 的绑定,很难看到是那里出错 如何做 vs 调试 binding?

如果界面很简单,当时忽然就和想的不一样,无论如何做都不会反应,但是难以知道是在哪写错,输出也没有显示,那么这时需要如何做?

可以在 binding 输出很多关于他如何做的,如何寻找绑定的数据,绑定是如何创建的

首先引用

代码语言:javascript
复制
          xmlns:trace="clr-namespace:System.Diagnostics;assembly=WindowsBase"

然后在binding里写 trace:PresentationTraceSources.TraceLevel=High

我在一个复杂的界面,很难知道是不是在哪写错,于是代码如下

代码语言:javascript
复制
    <TextBlock FontSize="48" HorizontalAlignment="Center" VerticalAlignment="Center"
                               Text="{Binding RelativeSource={RelativeSource AncestorType=ListBoxItem}, Path=(ItemsControl.AlternationIndex),trace:PresentationTraceSources.TraceLevel=High, Mode=OneWay, Converter={StaticResource NumberAddOne}}">

如果这个 binding 是在后台代码创建,那么请使用下面代码,这里 BakooteZuroolu 是一个 TextBlock ,把他的 Text 绑定到 KasxoujarGayher ,可以看到代码很少。

代码语言:javascript
复制
            var binding = new Binding(nameof(ViewModel.KasxoujarGayher));

            PresentationTraceSources.SetTraceLevel(binding, PresentationTraceLevel.High);

            BindingOperations.SetBinding(BakooteZuroolu, TextBlock.TextProperty, binding);

运行打开就可以看到输出这些代码

代码语言:javascript
复制
    System.Windows.Data Warning: 56 : Created BindingExpression (hash=36771698) for Binding (hash=7954964)
System.Windows.Data Warning: 58 :   Path: '(0)'
System.Windows.Data Warning: 61 : BindingExpression (hash=36771698): Default update trigger resolved to PropertyChanged
System.Windows.Data Warning: 56 : Created BindingExpression (hash=62509834) for Binding (hash=7954964)
System.Windows.Data Warning: 58 :   Path: '(0)'
System.Windows.Data Warning: 61 : BindingExpression (hash=62509834): Default update trigger resolved to PropertyChanged
System.Windows.Data Warning: 62 : BindingExpression (hash=62509834): Attach to System.Windows.Controls.TextBlock.Text (hash=8340425)
System.Windows.Data Warning: 66 : BindingExpression (hash=62509834): RelativeSource (FindAncestor) requires tree context
System.Windows.Data Warning: 65 : BindingExpression (hash=62509834): Resolve source deferred
System.Windows.Data Warning: 56 : Created BindingExpression (hash=55365254) for Binding (hash=37966223)
System.Windows.Data Warning: 58 :   Path: '(0)'
System.Windows.Data Warning: 61 : BindingExpression (hash=55365254): Default update trigger resolved to PropertyChanged
System.Windows.Data Warning: 56 : Created BindingExpression (hash=28525238) for Binding (hash=37966223)
System.Windows.Data Warning: 58 :   Path: '(0)'
System.Windows.Data Warning: 61 : BindingExpression (hash=28525238): Default update trigger resolved to PropertyChanged
System.Windows.Data Warning: 62 : BindingExpression (hash=28525238): Attach to System.Windows.Controls.TextBlock.Text (hash=48957711)
System.Windows.Data Warning: 66 : BindingExpression (hash=28525238): RelativeSource (FindAncestor) requires tree context
System.Windows.Data Warning: 65 : BindingExpression (hash=28525238): Resolve source deferred
System.Windows.Data Warning: 56 : Created BindingExpression (hash=16291455) for Binding (hash=40797849)
System.Windows.Data Warning: 58 :   Path: '(0)'
System.Windows.Data Warning: 61 : BindingExpression (hash=16291455): Default update trigger resolved to PropertyChanged
System.Windows.Data Warning: 56 : Created BindingExpression (hash=12405375) for Binding (hash=40797849)
System.Windows.Data Warning: 58 :   Path: '(0)'
System.Windows.Data Warning: 61 : BindingExpression (hash=12405375): Default update trigger resolved to PropertyChanged
System.Windows.Data Warning: 62 : BindingExpression (hash=12405375): Attach to System.Windows.Controls.TextBlock.Text (hash=34359256)
System.Windows.Data Warning: 66 : BindingExpression (hash=12405375): RelativeSource (FindAncestor) requires tree context
System.Windows.Data Warning: 65 : BindingExpression (hash=12405375): Resolve source deferred
System.Windows.Data Warning: 56 : Created BindingExpression (hash=6720246) for Binding (hash=42336695)
System.Windows.Data Warning: 58 :   Path: '(0)'
System.Windows.Data Warning: 61 : BindingExpression (hash=6720246): Default update trigger resolved to PropertyChanged
System.Windows.Data Warning: 56 : Created BindingExpression (hash=60482217) for Binding (hash=42336695)
System.Windows.Data Warning: 58 :   Path: '(0)'
System.Windows.Data Warning: 61 : BindingExpression (hash=60482217): Default update trigger resolved to PropertyChanged
System.Windows.Data Warning: 62 : BindingExpression (hash=60482217): Attach to System.Windows.Controls.TextBlock.Text (hash=64356400)
System.Windows.Data Warning: 66 : BindingExpression (hash=60482217): RelativeSource (FindAncestor) requires tree context
System.Windows.Data Warning: 65 : BindingExpression (hash=60482217): Resolve source deferred
System.Windows.Data Warning: 56 : Created BindingExpression (hash=29858833) for Binding (hash=64163473)
System.Windows.Data Warning: 58 :   Path: '(0)'
System.Windows.Data Warning: 61 : BindingExpression (hash=29858833): Default update trigger resolved to PropertyChanged
System.Windows.Data Warning: 56 : Created BindingExpression (hash=294043) for Binding (hash=64163473)
System.Windows.Data Warning: 58 :   Path: '(0)'
System.Windows.Data Warning: 61 : BindingExpression (hash=294043): Default update trigger resolved to PropertyChanged
System.Windows.Data Warning: 62 : BindingExpression (hash=294043): Attach to System.Windows.Controls.TextBlock.Text (hash=59325057)
System.Windows.Data Warning: 66 : BindingExpression (hash=294043): RelativeSource (FindAncestor) requires tree context
System.Windows.Data Warning: 65 : BindingExpression (hash=294043): Resolve source deferred
System.Windows.Data Warning: 67 : BindingExpression (hash=62509834): Resolving source 
System.Windows.Data Warning: 70 : BindingExpression (hash=62509834): Found data context element: <null> (OK)
System.Windows.Data Warning: 73 :     Lookup ancestor of type ListBoxItem:  queried Grid (hash=64665535)
System.Windows.Data Warning: 73 :     Lookup ancestor of type ListBoxItem:  queried Grid (hash=22098140)
System.Windows.Data Warning: 73 :     Lookup ancestor of type ListBoxItem:  queried ContentPresenter (hash=9911889)
System.Windows.Data Warning: 73 :     Lookup ancestor of type ListBoxItem:  queried Border (hash=8557861)
System.Windows.Data Warning: 73 :     Lookup ancestor of type ListBoxItem:  queried ListViewItem (hash=48873995)
System.Windows.Data Warning: 72 :   RelativeSource.FindAncestor found ListViewItem (hash=48873995)
System.Windows.Data Warning: 78 : BindingExpression (hash=62509834): Activate with root item ListViewItem (hash=48873995)
System.Windows.Data Warning: 108 : BindingExpression (hash=62509834):   At level 0 - for ListViewItem.(ItemsControl.AlternationIndex) found accessor DependencyProperty(AlternationIndex)
System.Windows.Data Warning: 104 : BindingExpression (hash=62509834): Replace item at level 0 with ListViewItem (hash=48873995), using accessor DependencyProperty(AlternationIndex)
System.Windows.Data Warning: 101 : BindingExpression (hash=62509834): GetValue at level 0 from ListViewItem (hash=48873995) using DependencyProperty(AlternationIndex): '0'
System.Windows.Data Warning: 80 : BindingExpression (hash=62509834): TransferValue - got raw value '0'
System.Windows.Data Warning: 82 : BindingExpression (hash=62509834): TransferValue - user's converter produced '1'
System.Windows.Data Warning: 84 : BindingExpression (hash=62509834): TransferValue - implicit converter produced '1'
System.Windows.Data Warning: 89 : BindingExpression (hash=62509834): TransferValue - using final value '1'
System.Windows.Data Warning: 67 : BindingExpression (hash=28525238): Resolving source 
System.Windows.Data Warning: 70 : BindingExpression (hash=28525238): Found data context element: <null> (OK)
System.Windows.Data Warning: 73 :     Lookup ancestor of type ListBoxItem:  queried Grid (hash=42455531)
System.Windows.Data Warning: 73 :     Lookup ancestor of type ListBoxItem:  queried Grid (hash=56913064)
System.Windows.Data Warning: 73 :     Lookup ancestor of type ListBoxItem:  queried ContentPresenter (hash=13780214)
System.Windows.Data Warning: 73 :     Lookup ancestor of type ListBoxItem:  queried Border (hash=23900756)
System.Windows.Data Warning: 73 :     Lookup ancestor of type ListBoxItem:  queried ListViewItem (hash=37212772)
System.Windows.Data Warning: 72 :   RelativeSource.FindAncestor found ListViewItem (hash=37212772)
System.Windows.Data Warning: 78 : BindingExpression (hash=28525238): Activate with root item ListViewItem (hash=37212772)
System.Windows.Data Warning: 107 : BindingExpression (hash=28525238):   At level 0 using cached accessor for ListViewItem.(ItemsControl.AlternationIndex): DependencyProperty(AlternationIndex)
System.Windows.Data Warning: 104 : BindingExpression (hash=28525238): Replace item at level 0 with ListViewItem (hash=37212772), using accessor DependencyProperty(AlternationIndex)
System.Windows.Data Warning: 101 : BindingExpression (hash=28525238): GetValue at level 0 from ListViewItem (hash=37212772) using DependencyProperty(AlternationIndex): '1'
System.Windows.Data Warning: 80 : BindingExpression (hash=28525238): TransferValue - got raw value '1'
System.Windows.Data Warning: 82 : BindingExpression (hash=28525238): TransferValue - user's converter produced '2'
System.Windows.Data Warning: 84 : BindingExpression (hash=28525238): TransferValue - implicit converter produced '2'
System.Windows.Data Warning: 89 : BindingExpression (hash=28525238): TransferValue - using final value '2'
System.Windows.Data Warning: 67 : BindingExpression (hash=12405375): Resolving source 
System.Windows.Data Warning: 70 : BindingExpression (hash=12405375): Found data context element: <null> (OK)
System.Windows.Data Warning: 73 :     Lookup ancestor of type ListBoxItem:  queried Grid (hash=23387474)
System.Windows.Data Warning: 73 :     Lookup ancestor of type ListBoxItem:  queried Grid (hash=2598608)
System.Windows.Data Warning: 73 :     Lookup ancestor of type ListBoxItem:  queried ContentPresenter (hash=45027976)
System.Windows.Data Warning: 73 :     Lookup ancestor of type ListBoxItem:  queried Border (hash=57198891)
System.Windows.Data Warning: 73 :     Lookup ancestor of type ListBoxItem:  queried ListViewItem (hash=66479500)
System.Windows.Data Warning: 72 :   RelativeSource.FindAncestor found ListViewItem (hash=66479500)
System.Windows.Data Warning: 78 : BindingExpression (hash=12405375): Activate with root item ListViewItem (hash=66479500)
System.Windows.Data Warning: 107 : BindingExpression (hash=12405375):   At level 0 using cached accessor for ListViewItem.(ItemsControl.AlternationIndex): DependencyProperty(AlternationIndex)
System.Windows.Data Warning: 104 : BindingExpression (hash=12405375): Replace item at level 0 with ListViewItem (hash=66479500), using accessor DependencyProperty(AlternationIndex)
System.Windows.Data Warning: 101 : BindingExpression (hash=12405375): GetValue at level 0 from ListViewItem (hash=66479500) using DependencyProperty(AlternationIndex): '2'
System.Windows.Data Warning: 80 : BindingExpression (hash=12405375): TransferValue - got raw value '2'
System.Windows.Data Warning: 82 : BindingExpression (hash=12405375): TransferValue - user's converter produced '3'
System.Windows.Data Warning: 84 : BindingExpression (hash=12405375): TransferValue - implicit converter produced '3'
System.Windows.Data Warning: 89 : BindingExpression (hash=12405375): TransferValue - using final value '3'
System.Windows.Data Warning: 67 : BindingExpression (hash=60482217): Resolving source 
System.Windows.Data Warning: 70 : BindingExpression (hash=60482217): Found data context element: <null> (OK)
System.Windows.Data Warning: 73 :     Lookup ancestor of type ListBoxItem:  queried Grid (hash=47357306)
System.Windows.Data Warning: 73 :     Lookup ancestor of type ListBoxItem:  queried Grid (hash=35088084)
System.Windows.Data Warning: 73 :     Lookup ancestor of type ListBoxItem:  queried ContentPresenter (hash=3898676)
System.Windows.Data Warning: 73 :     Lookup ancestor of type ListBoxItem:  queried Border (hash=22802807)
System.Windows.Data Warning: 73 :     Lookup ancestor of type ListBoxItem:  queried ListViewItem (hash=61444595)
System.Windows.Data Warning: 72 :   RelativeSource.FindAncestor found ListViewItem (hash=61444595)
System.Windows.Data Warning: 78 : BindingExpression (hash=60482217): Activate with root item ListViewItem (hash=61444595)
System.Windows.Data Warning: 107 : BindingExpression (hash=60482217):   At level 0 using cached accessor for ListViewItem.(ItemsControl.AlternationIndex): DependencyProperty(AlternationIndex)
System.Windows.Data Warning: 104 : BindingExpression (hash=60482217): Replace item at level 0 with ListViewItem (hash=61444595), using accessor DependencyProperty(AlternationIndex)
System.Windows.Data Warning: 101 : BindingExpression (hash=60482217): GetValue at level 0 from ListViewItem (hash=61444595) using DependencyProperty(AlternationIndex): '3'
System.Windows.Data Warning: 80 : BindingExpression (hash=60482217): TransferValue - got raw value '3'
System.Windows.Data Warning: 82 : BindingExpression (hash=60482217): TransferValue - user's converter produced '4'
System.Windows.Data Warning: 84 : BindingExpression (hash=60482217): TransferValue - implicit converter produced '4'
System.Windows.Data Warning: 89 : BindingExpression (hash=60482217): TransferValue - using final value '4'
System.Windows.Data Warning: 67 : BindingExpression (hash=294043): Resolving source 
System.Windows.Data Warning: 70 : BindingExpression (hash=294043): Found data context element: <null> (OK)
System.Windows.Data Warning: 73 :     Lookup ancestor of type ListBoxItem:  queried Grid (hash=45699530)
System.Windows.Data Warning: 73 :     Lookup ancestor of type ListBoxItem:  queried Grid (hash=5077725)
System.Windows.Data Warning: 73 :     Lookup ancestor of type ListBoxItem:  queried ContentPresenter (hash=564191)
System.Windows.Data Warning: 73 :     Lookup ancestor of type ListBoxItem:  queried Border (hash=62687)
System.Windows.Data Warning: 73 :     Lookup ancestor of type ListBoxItem:  queried ListViewItem (hash=16130451)
System.Windows.Data Warning: 72 :   RelativeSource.FindAncestor found ListViewItem (hash=16130451)
System.Windows.Data Warning: 78 : BindingExpression (hash=294043): Activate with root item ListViewItem (hash=16130451)
System.Windows.Data Warning: 107 : BindingExpression (hash=294043):   At level 0 using cached accessor for ListViewItem.(ItemsControl.AlternationIndex): DependencyProperty(AlternationIndex)
System.Windows.Data Warning: 104 : BindingExpression (hash=294043): Replace item at level 0 with ListViewItem (hash=16130451), using accessor DependencyProperty(AlternationIndex)

参见:http://jimmangaly.blogspot.com/2009/04/debugging-data-binding-in-wpf.html


本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档