Debug Databinding Issues in WPF
2013-08-14 22:21
591 查看
DataBindingisoneofthemostpowerfulfeaturesinWPF.Butbecauseitresolvesthebindingsatruntimeanddoesnotthrowexceptions,it'ssometimeshardtofindthereasonwhythedatadonotappearasexpected.Therearemainlytworeasons:
TheDataBindingexpressionisinvalid.ThenuseTraceOutputtoresolve.
TheDataBindingexpressionisvalid,buttheresultisnottheexpected.ThenuseaDebugConvertertoresolveit.
Inthiscasetheinvaliddatabindingexpressionisreportedbyatracemessageintheoutputwindow
Note:BindingtoapathofapropertythathasNULLvalueisavalidexpressionanddoesnotgenerateanerrormessage(fore.g.bindingtoapropertyofthedatacontextthatisNULL).
TosetthetracelevelyouhavetoincludeanextranamespacetoyourXAML:
Thefollowingsnippedshowshowtoadjustthetracelevelbycode:
TousetheconverterinXAML,referencethenamespaceoftheassemblythatcontainstheconverterandaddaninstanceofittotheresourcesofyourwindow.
comefromhttp://www.wpftutorial.net/DebugDataBinding.html
TheDataBindingexpressionisinvalid.ThenuseTraceOutputtoresolve.
TheDataBindingexpressionisvalid,buttheresultisnottheexpected.ThenuseaDebugConvertertoresolveit.
Method1:Tracemessagesintheoutputwindow
Intheexample,thetextpropertyoftheTextBlockisboundtotheproperty"InvalidPath"oftheStackPanel-whichdoesnotexists.<Windowx:Class="DebugDataBinding.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <StackPanelx:Name="stack"> <TextBlockText="{BindingElementName=stack,Path=InvalidPath}"/> </StackPanel> </Window>
Inthiscasetheinvaliddatabindingexpressionisreportedbyatracemessageintheoutputwindow
System.Windows.DataError:39:BindingExpressionpatherror:'InvalidPath'propertynotfoundon'object'''StackPanel'(Name='stack')'.BindingExpression:Path=InvalidPath;DataItem='StackPanel'(Name='stack');targetelementis'TextBlock'(Name='');targetpropertyis'Text'(type'String')
Note:BindingtoapathofapropertythathasNULLvalueisavalidexpressionanddoesnotgenerateanerrormessage(fore.g.bindingtoapropertyofthedatacontextthatisNULL).
Adjustthetracelevel(.NET3.5andhigher)
NET3.5hasanewfeaturethatallowsyoutosetthelevelofdetailsoftracemessagestoNone,
Low,
Mediumor
High.
TosetthetracelevelyouhavetoincludeanextranamespacetoyourXAML:
<Windowx:Class="DebugDataBinding.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:diag="clr-namespace:System.Diagnostics;assembly=WindowsBase">
<StackPanelx:Name="stack">
<TextBlockText="{BindingElementName=stack,Path=InvalidPath,
diag:PresentationTraceSources.TraceLevel=High}"/>
</StackPanel>
</Window>
Thefollowingsnippedshowshowtoadjustthetracelevelbycode:
PresentationTraceSources.DataBindingSource.Listeners.Add(
newConsoleTraceListener());
PresentationTraceSources.DataBindingSource.Switch.Level=SourceLevels.All;
Method2:UseaValueConvertertobreakintothedebugger
Asimpletrickistowriteavalueconverterthatdoesnothinsexceptbreakingintothedebugger.Allyouneedtodonowistoaddthisconvertertothebindingexpressionthatfailsandyoucaneasilyseethevaluesthatshouldbebound.///<summary>
///Thisconverterdoesnothingexceptbreakingthe
///debuggerintotheconvertmethod
///</summary>
publicclassDatabindingDebugConverter:IValueConverter
{
publicobjectConvert(objectvalue,TypetargetType,
objectparameter,CultureInfoculture)
{
Debugger.Break();
returnvalue;
}
publicobjectConvertBack(objectvalue,TypetargetType,
objectparameter,CultureInfoculture)
{
Debugger.Break();
returnvalue;
}
}
TousetheconverterinXAML,referencethenamespaceoftheassemblythatcontainstheconverterandaddaninstanceofittotheresourcesofyourwindow.
<Windowx:Class="DebugDataBinding.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:DebugDataBinding"
Title="Window1"Height="300"Width="300">
<Window.Resources>
<local:DatabindingDebugConverterx:Key="debugConverter"/>
</Window.Resources>
<StackPanelx:Name="stack">
<TextBlockText="{BindingElementName=stack,Path=ActualWidth,
Converter={StaticResourcedebugConverter}}"/>
</StackPanel>
</Window>
comefrom
相关文章推荐
- Debug Databinding Issues in WPF
- Debugging Data Binding in WPF
- Data Binding in WPF
- Data Binding In WPF
- csharp: Data binding in WPF DataGrid control
- Data Binding in WPF
- csharp: Data binding in WPF DataGrid control
- csharp: Data binding in WPF DataGrid control
- Databinding in WPF
- How can I create a data binding in code using WPF?
- WPF DataBinding
- Simple Data Binding in Silverlight 4.0
- silverlight wpf DataTemplate Command binding
- Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
- DisplayMemberPath 与 DataTemplate - WPF data binding 1
- Data Virtualization in WPF and beyond
- Data Binding: A godsend or the devil in disguise?
- WP7 LongListSelector in depth | Part2: Data binding scenarios
- WPF DataBinding some other details
- WPF Tips: Data Binding Converter一例:bool与IsChecked属性(bool?)之间的转换