2019-9-18-WPF-如何调试-binding
| title | author | date | CreateTime | categories |
|---|---|---|---|---|
|
WPF 如何调试 binding
|
lindexi
|
2019-9-18 10:1:4 +0800
|
2018-2-13 17:23:3 +0800
|
WPF WPF调试 调试
|
如果是写在 xaml 的绑定,很难看到是那里出错。本文告诉大家如何在 vs 调试 binding 是否绑定上?
在写 Binding 的时候,需要在运行的时候才能知道是否绑定对了,但是可能有时候代码是好的,但是有逗比小伙伴改了一下就不知道是属性名修改了绑定不上,还是转换器没写对
在 WPF 提供了输出绑定的信息,可以在 binding 输出很多关于他如何做的,如何寻找绑定的数据,绑定是如何创建的
本文告诉大家如何使用绑定输出
首先引用命名空间,请在添加 trace 命名空间
xmlns:trace="clr-namespace:System.Diagnostics;assembly=WindowsBase"
然后在 binding 里写 trace:PresentationTraceSources.TraceLevel=High 设置调试等级
我在一个复杂的界面,很难知道是不是在哪写错,在绑定里面添加输出的方法如下
-
<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 一个我也不知道什么的属性,可以看到代码很少。
var binding = new Binding(nameof(ViewModel.KasxoujarGayher));
PresentationTraceSources.SetTraceLevel(binding, PresentationTraceLevel.High);
BindingOperations.SetBinding(BakooteZuroolu, TextBlock.TextProperty, binding);
运行打开就可以看到输出这些代码
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
如果是想要在 VisualStudio 输出所有绑定的详细信息请看 WPF 笔刷绑定不上可能的原因
2019-9-18-WPF-如何调试-binding的更多相关文章
- 2019.3.18考试&2019.3.19考试&2019.3.21考试
2019.3.18 C O D E T1 树上直接贪心,环上for一遍贪心 哇说的简单,码了将近一下午终于码出来了 感觉自己码力/写题策略太糟糕了,先是搞了一个细节太多的写法最后不得不弃疗了,然后第二 ...
- 【转】WPF中的Binding技巧(二)
WPF中的Binding技巧(二) 接上篇, 我们来看一看Elementname,Source,RelativeSource 三种绑定的方式 1.ElementName顾名思义就是根据Ui元素 ...
- WPF性能调试系列 – 内存监测
WPF性能调试系列文章: WPF页面渲染优化:Application Timeline WPF页面业务加载优化:Ants Performance Profiler WPF内存优化:Ants M ...
- WPF性能调试系列 – Ants Performance Profiler
WPF性能调试系列文章: WPF页面渲染优化:Application Timeline WPF页面业务加载优化:Ants Performance Profiler WPF内存优化:Ants Memor ...
- WPF性能调试系列 – 应用程序时间线
WPF性能调试系列文章: WPF页面渲染优化:Application Timeline WPF页面业务加载优化:Ants Performance Profiler WPF内存优化:Ants Memor ...
- [WPF]如何调试Data Binding
前言 在WPF开发中,将ViewModel中对象绑定到UI上时,会出现明明已经将数据对象Binding到UI,但是UI上就是不显示等等的问题.这篇博客将介绍WPF Data Binding相关的内容, ...
- [XAML]类似WPF绑定的Binding的读取方法
在WPF的XAML里,依赖属性可以使用基于BindingBase之类的MarkupExtensin 读取XAML时,会自动的把该BindingBase转换为BindingExpressionBase ...
- [WPF系列]-使用Binding来同步不同控件的Dependency property
简介 项目中经常会用到,同步两个控件的值,本文就简单列举两种方式来同步不同控件的两个Dependency Property. 示例 效果图: 只使用C#代码: //获取slider1的ValueDep ...
- (WPF, MVVM) Slider Binding.
对于Button的Command的绑定可以通过实现ICommand接口来进行,但是Slider并没有Command属性. 另外如果要实现MVVM模式的话,需要将一些Method和Slider的Even ...
随机推荐
- JS中for,for...in,for...of以及foreach循环的用法
1.for()循环 // for循环的表达式之间用的是;号分隔的,千万不要写成, for (初始化表达式1; 判断表达式2; 自增表达式3) { // 循环体4 } 2.for...in索引遍历 va ...
- 「SAP技术」SAP MM 事务代码ME17的用法
SAP MM 事务代码ME17的用法 1,如下采购信息记录需要被归档: PIR号码,5300007816 2, ME12打上删除标记, 3, 事务代码ME17做归档 3.1 创建archive ...
- Gradle task简单使用
还望支持个人博客站:http://www.enjoytoday.cn task是什么 task是gradle构建脚本的最小运行单元,我们通过在gradle脚本中创建task任务,以期完成某个特定的功能 ...
- Python—内置三大装饰器(@staticmethod、@classmethod、@property)
https://blog.csdn.net/weixin_42681866/article/details/83376484 https://blog.csdn.net/weixin_43265804 ...
- C++ 基础语法 快速复习笔记(3)---重载函数,多态,虚函数
1.重载运算符和重载函数: C++ 允许在同一作用域中的某个函数和运算符指定多个定义,分别称为函数重载和运算符重载. 重载声明是指一个与之前已经在该作用域内声明过的函数或方法具有相同名称的声明,但是它 ...
- python 连接数据库,查询结果写入数据到excel
使用Python链接数据库查询数据,并将查询结果写入到Excel中,实现方法上主要有两步,第一,查数据,第二,写Excel. 一.导入需要的包 import time import xlwt from ...
- mysql里字符集的配置
[client]default-character-set=utf8[mysqld]character-set-server = utf8[mysql]default-character-set=ut ...
- 01 less的使用
使用less 安装两个包 1===>cnpm install less less-loader --save-dev less中的注释 以 //开头的注释 不会被编译到css文件中去 以 /** ...
- 【洛谷P1963】[NOI2009]变换序列(二分图匹配)
传送门 题意: 现有一个\(0\)到\(n-1\)的排列\(T\),定义距离\(D(x,y)=min\{|x-y|,N-|x-y|\}\). 现在给出\(D(i, T_i)\),输出字典序最小的符合条 ...
- excel工资表,转化为word工资单
1.新建excel表,.xls格式; .xlsx格式的添加到word中会报错‘无法打开数据源’(office 2007): 2.新建word文档.docx格式: 3.邮件——开始邮件合并——信函,选择 ...