×
Namespaces

Variants
Actions
(Difference between revisions)

How to debug data binding problems on Windows Phone

From Nokia Developer Wiki
Jump to: navigation, search
influencer (Talk | contribs)
(Influencer -)
 
influencer (Talk | contribs)
(Influencer -)
Line 1: Line 1:
 
[[Category:Draft]][[Category:Windows Phone]][[Category:Silverlight]]
 
[[Category:Draft]][[Category:Windows Phone]][[Category:Silverlight]]
''Delete instructional text in italic''
+
{{Abstract|Often XAML elements with data binding show no data during development. This article shows one method how to find the source of this problem. }}
  
{{Abstract|This article explains how to ... }} ''Replace the abstract text with a short paragraph (or sentence) describing what the topic covers.''
 
 
''Enter article metadata as described below. Note that this template can be placed anywhere in the article. Do not remove parameters that you do not use''
 
 
{{ArticleMetaData <!-- v1.2 -->
 
{{ArticleMetaData <!-- v1.2 -->
 
|sourcecode= <!-- Link to example source code e.g. [[Media:The Code Example ZIP.zip]] -->
 
|sourcecode= <!-- Link to example source code e.g. [[Media:The Code Example ZIP.zip]] -->
Line 35: Line 32:
 
Say you have a ListBox like this:
 
Say you have a ListBox like this:
  
 +
<code xml>
 
         <ListBox x:Name="MainListBox" Margin="0,0,-12,0" ItemsSource="{Binding Items}" SelectionChanged="MainListBox_SelectionChanged">
 
         <ListBox x:Name="MainListBox" Margin="0,0,-12,0" ItemsSource="{Binding Items}" SelectionChanged="MainListBox_SelectionChanged">
 
             <ListBox.ItemTemplate>
 
             <ListBox.ItemTemplate>
Line 45: Line 43:
 
             </ListBox.ItemTemplate>
 
             </ListBox.ItemTemplate>
 
         </ListBox>
 
         </ListBox>
 +
</code>
  
 
You can add a converter that gets called when the data from the binding source arrives and before it gets delivered to the control. Here we have one condition when this debugging technique can't help: if the binding fails earlier, the converter doesn't get calledt
 
You can add a converter that gets called when the data from the binding source arrives and before it gets delivered to the control. Here we have one condition when this debugging technique can't help: if the binding fails earlier, the converter doesn't get calledt
Line 50: Line 49:
 
To implement the converter you have to derive a class from IValueConverter and implement it's methods:
 
To implement the converter you have to derive a class from IValueConverter and implement it's methods:
  
 +
<code csharp>
 
   public class DatabindingDebugConverter : IValueConverter
 
   public class DatabindingDebugConverter : IValueConverter
 
   {
 
   {
Line 64: Line 64:
 
       }
 
       }
 
   }
 
   }
 
+
</code>
  
 
Then you have to declare the converter as a static resource in your page's xaml:
 
Then you have to declare the converter as a static resource in your page's xaml:
  
 +
<code xml>
 
   <phone:PhoneApplicationPage.Resources>
 
   <phone:PhoneApplicationPage.Resources>
 
       <local:DataBindingDebugConverter x:Key="DatabindingDebugConverter"/>
 
       <local:DataBindingDebugConverter x:Key="DatabindingDebugConverter"/>
 
   </phone:PhoneApplicationPage.Resources>
 
   </phone:PhoneApplicationPage.Resources>
 +
</code>
  
 
assuming you have declared the local namespace
 
assuming you have declared the local namespace
  
 +
<code xml>
 
     xmlns:local="clr-namespace:EncryptionSample"
 
     xmlns:local="clr-namespace:EncryptionSample"
 +
</code>
  
 
in the page tag and call it in the data binding statement:
 
in the page tag and call it in the data binding statement:
  
                     <TextBlock Text="{Binding LineOne, Converter={StaticResource DataBindingDebugConverter}}" TextWrapping="Wrap" Style="{StaticResource  
+
<code xml>
 
+
                     <TextBlock Text="{Binding LineOne, Converter={StaticResource DataBindingDebugConverter}}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
PhoneTextExtraLargeStyle}"/>
+
</code>
  
 
Next you set a breakpoint in the Convert method and voilà, you can see the values that arrive from binding, if any.
 
Next you set a breakpoint in the Convert method and voilà, you can see the values that arrive from binding, if any.
 +
 +
== Sample code ==
  
 
A sample project is in the works, please call again later...
 
A sample project is in the works, please call again later...
 
 
 
 
== Summary ==
 
 
 
   
 
   
''Remove Category:Draft when the page is complete or near complete''
 
 
 
''The "platform categories" will be displayed here in '''preview''' only - Copy paste relevant categories into text here''
 
 
 
{{VersionHint|Versions will be visible from this template when viewed in preview. You can delete this or leave it in the page as it is not displayed in final version}}
 
{{VersionHint|Versions will be visible from this template when viewed in preview. You can delete this or leave it in the page as it is not displayed in final version}}
 
''Add categories below using category selector.''
 

Revision as of 22:44, 12 October 2012

Often XAML elements with data binding show no data during development. This article shows one method how to find the source of this problem.

WP Metro Icon File.png
WP Metro Icon Bug.png
SignpostIcon XAML 40.png
WP Metro Icon WP8.png
SignpostIcon WP7 70px.png
Article Metadata
CompatibilityArticle
Created: influencer (12 Oct 2012)
Last edited: influencer (12 Oct 2012)

Introduction

If you use data binding in your Windows Phone application, during development you'll come to the point where you have linked everything together but data doesn't display in your control or the wrong data appears.

What can you do? You can use an IValueConverter and set a breakpoint there. This way you can see what data arrives at your element and check the available properties.

Say you have a ListBox like this:

         <ListBox x:Name="MainListBox" Margin="0,0,-12,0" ItemsSource="{Binding Items}" SelectionChanged="MainListBox_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="0,0,0,17" Width="432" Height="78">
<TextBlock Text="{Binding LineOne}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>
<TextBlock Text="{Binding LineTwo}" TextWrapping="Wrap" Margin="12,-6,12,0" Style="{StaticResource PhoneTextSubtleStyle}"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>

You can add a converter that gets called when the data from the binding source arrives and before it gets delivered to the control. Here we have one condition when this debugging technique can't help: if the binding fails earlier, the converter doesn't get calledt

To implement the converter you have to derive a class from IValueConverter and implement it's methods:

   public class DatabindingDebugConverter : IValueConverter
{
public object Convert(object value, Type targetType,
object parameter, string language)
{
return value;
}
 
public object ConvertBack(object value, Type targetType,
object parameter, string language)
{
return value;
}
}

Then you have to declare the converter as a static resource in your page's xaml:

   <phone:PhoneApplicationPage.Resources>
<local:DataBindingDebugConverter x:Key="DatabindingDebugConverter"/>
</phone:PhoneApplicationPage.Resources>

assuming you have declared the local namespace

    xmlns:local="clr-namespace:EncryptionSample"

in the page tag and call it in the data binding statement:

                     <TextBlock Text="{Binding LineOne, Converter={StaticResource DataBindingDebugConverter}}" TextWrapping="Wrap" Style="{StaticResource PhoneTextExtraLargeStyle}"/>

Next you set a breakpoint in the Convert method and voilà, you can see the values that arrive from binding, if any.

Sample code

A sample project is in the works, please call again later...

Version Hint

Windows Phone: [[Category:Windows Phone]]
[[Category:Windows Phone 7.5]]
[[Category:Windows Phone 8]]

Nokia Asha: [[Category:Nokia Asha]]
[[Category:Nokia Asha Platform 1.0]]

Series 40: [[Category:Series 40]]
[[Category:Series 40 1st Edition]] [[Category:Series 40 2nd Edition]]
[[Category:Series 40 3rd Edition (initial release)]] [[Category:Series 40 3rd Edition FP1]] [[Category:Series 40 3rd Edition FP2]]
[[Category:Series 40 5th Edition (initial release)]] [[Category:Series 40 5th Edition FP1]]
[[Category:Series 40 6th Edition (initial release)]] [[Category:Series 40 6th Edition FP1]] [[Category:Series 40 Developer Platform 1.0]] [[Category:Series 40 Developer Platform 1.1]] [[Category:Series 40 Developer Platform 2.0]]

Symbian: [[Category:Symbian]]
[[Category:S60 1st Edition]] [[Category:S60 2nd Edition (initial release)]] [[Category:S60 2nd Edition FP1]] [[Category:S60 2nd Edition FP2]] [[Category:S60 2nd Edition FP3]]
[[Category:S60 3rd Edition (initial release)]] [[Category:S60 3rd Edition FP1]] [[Category:S60 3rd Edition FP2]]
[[Category:S60 5th Edition]]
[[Category:Symbian^3]] [[Category:Symbian Anna]] [[Category:Nokia Belle]]

184 page views in the last 30 days.
×