您的位置:首页 > 产品设计 > UI/UE

Debug Databinding Issues in WPF

2013-08-14 22:21 591 查看
DataBindingisoneofthemostpowerfulfeaturesinWPF.Butbecauseitresolvesthebindingsatruntimeanddoesnotthrowexceptions,it'ssometimeshardtofindthereasonwhythedatadonotappearasexpected.Therearemainlytworeasons:

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.5hasanewfeaturethatallowsyoutosetthelevelofdetailsoftracemessagesto
None
,
Low
,
Medium
or
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>


comefromhttp://www.wpftutorial.net/DebugDataBinding.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: