×
Namespaces

Variants
Actions
(Difference between revisions)

Collection of Value converters for Windows Phone apps

From Nokia Developer Wiki
Jump to: navigation, search
influencer (Talk | contribs)
(Influencer -)
 
influencer (Talk | contribs)
(Influencer -)
Line 3: Line 3:
  
 
{{ArticleMetaData <!-- v1.2 -->
 
{{ArticleMetaData <!-- v1.2 -->
|sourcecode= [[Media: ValueConverterSample]]
+
|sourcecode= [[Media: ValueConverterSample.zip]]
 
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 
|devices= <!-- Devices tested against - e.g. ''devices=Nokia 6131 NFC, Nokia C7-00'') -->
 
|devices= <!-- Devices tested against - e.g. ''devices=Nokia 6131 NFC, Nokia C7-00'') -->
Line 28: Line 28:
 
Value converters are a versatile instrument helping in data binding scenarios on the Windows Phone (and Windows 8 Modern UI or WPF) platforms. The spectrum includes
 
Value converters are a versatile instrument helping in data binding scenarios on the Windows Phone (and Windows 8 Modern UI or WPF) platforms. The spectrum includes
  
* data type conversions, e.g. BooleanToVisibilityConverter
+
* data type conversions, e.g. {{Icode|BooleanToVisibilityConverter}}
* operations on bound data, e.g. BooleanNegationConverter
+
* operations on bound data, e.g. {{Icode|BooleanNegationConverter}}
* special effects, like drawing dynamic images, e.g. TimeToShapeConverter
+
* special effects, like drawing dynamic images, e.g. {{Icode|TimeToShapeConverter}}
* debugging purposes, e.g. DatabindingDebugConverter see [[http://www.developer.nokia.com/Community/Wiki/How_to_debug_data_binding_problems_on_Windows_Phone How to debug data binding problems on Windows Phone]]
+
* debugging purposes, e.g.{{Icode| DatabindingDebugConverter}} see [[http://www.developer.nokia.com/Community/Wiki/How_to_debug_data_binding_problems_on_Windows_Phone How to debug data binding problems on Windows Phone]]
  
  
Line 38: Line 38:
 
[[file: ConverterScreen.png]]
 
[[file: ConverterScreen.png]]
  
Note: The [http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter.aspx IValueConverter interface] is defined with different function parameter in Win 8 Modern UI. That's the reason I couldn't use a [http://msdn.microsoft.com/en-us/library/gg597391.aspx Portable Class Library] to contain the converter classes.
+
{{Note|The [http://msdn.microsoft.com/en-us/library/system.windows.data.ivalueconverter.aspx IValueConverter interface] is defined with different function parameters in Win 8 Modern UI aka "Metro" aka Windows 8 Store Apps. That's the reason I couldn't use a [http://msdn.microsoft.com/en-us/library/gg597391.aspx Portable Class Library] to contain the converter classes.}}
  
 
== Value Converter examples ==
 
== Value Converter examples ==
Line 150: Line 150:
  
 
== Sample code ==
 
== Sample code ==
This article is accompagnied by a [[Media: ValueConverterSample]] that just uses the two time converters to show the current time as a string and as an analog clock.
+
This article is accompagnied by a [[Media: ValueConverterSample.zip]] that just uses the two time converters to show the current time as a string and as an analog clock.
  
 
{{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 00:25, 27 October 2012

This article gives some examples for value converters that can be used in different scenarios in Windows Phone apps

WP Metro Icon File.png
WP Metro Icon UI.png
SignpostIcon XAML 40.png
WP Metro Icon WP8.png
SignpostIcon WP7 70px.png
Article Metadata
Code ExampleCompatibilityArticle
Created: influencer (26 Oct 2012)
Last edited: influencer (27 Oct 2012)

Contents

Introduction

Value converters are a versatile instrument helping in data binding scenarios on the Windows Phone (and Windows 8 Modern UI or WPF) platforms. The spectrum includes

  • data type conversions, e.g. BooleanToVisibilityConverter
  • operations on bound data, e.g. BooleanNegationConverter
  • special effects, like drawing dynamic images, e.g. TimeToShapeConverter
  • debugging purposes, e.g. DatabindingDebugConverter see [How to debug data binding problems on Windows Phone]


An introduction into the necessary steps to use the converters is given in the same article.

ConverterScreen.png

Note.pngNote: The IValueConverter interface is defined with different function parameters in Win 8 Modern UI aka "Metro" aka Windows 8 Store Apps. That's the reason I couldn't use a Portable Class Library to contain the converter classes.

Value Converter examples

/// <summary>
/// Value Converter that does nothing except giving the chance to set a breakpoint.
/// </summary>
public sealed 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;
}
}
 
 
/// <summary>
/// Value converter that translates true to false and vice versa.
/// </summary>
public sealed class BooleanNegationConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
return !(value is bool && (bool)value);
}
 
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
return !(value is bool && (bool)value);
}
}
 
 
/// <summary>
/// Value converter that translates true to <see cref="Visibility.Visible"/> and false to
/// <see cref="Visibility.Collapsed"/>.
/// </summary>
public sealed class BooleanToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
return (value is bool && (bool)value) ? Visibility.Visible : Visibility.Collapsed;
}
 
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
return value is Visibility && (Visibility)value == Visibility.Visible;
}
}
 
 
/// <summary>
/// Value converter that creates a clock image from a time value.
/// Reference the System.Windows.Media namespace.
/// You can bind the result to a Path element.
/// </summary>
public sealed class TimeToShapeConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
DateTime dt = (DateTime)value;
GeometryGroup coll = new GeometryGroup();
EllipseGeometry ell = new EllipseGeometry();
ell.Center = new Point(55, 55);
ell.RadiusX = ell.RadiusY = 60;
coll.Children.Add(ell);
LineGeometry hour = new LineGeometry();
double deg = (dt.Hour % 12) * Math.PI / 6;
hour.StartPoint = ell.Center;
hour.EndPoint = new Point(55 + Math.Sin(deg) * 35, 55 - Math.Cos(deg) * 35);
coll.Children.Add(hour);
LineGeometry minute = new LineGeometry();
minute.StartPoint = ell.Center;
deg = dt.Minute * Math.PI / 30;
minute.EndPoint = new Point(55 + Math.Sin(deg) * 50, 55 - Math.Cos(deg) * 50);
coll.Children.Add(minute);
return coll;
}
 
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
 
 
/// <summary>
/// Value converter that converts a DateTime to a text and vice versa.
/// </summary>
public sealed class TimeToStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
DateTime dt = (DateTime)value;
return dt.ToString();
}
 
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
DateTime dt;
DateTime.TryParse(value.ToString(), out dt);
return dt;
}
}

Sample code

This article is accompagnied by a Media: ValueConverterSample.zip that just uses the two time converters to show the current time as a string and as an analog clock.

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]]

555 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×