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

Very good way of formatting data. To implement the interface System.Windows.Data.IValueConverter

2010-06-03 21:40 471 查看
Here is a very good way of formatting data in Silverlight.

1. Firstly, you need to implement the interface System.Windows.Data.IValueConverter. The example is to show how to convert a number to percentage.

namespace FinancePCFlowTool.cs
{
public class PercentConverter : System.Windows.Data.IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null)
value = 0;
return string.Format("{0:N0}%", (double)value * 100);
}

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value == null)
return 0;
else
return double.Parse(value.ToString().Trim('%'));
}
}
}


2. After creating this class, you need to add a instance to application resources.

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="FinancePCFlowTool.App"
xmlns:cs="clr-namespace:FinancePCFlowTool.cs"
>
<Application.Resources>
<cs:PercentConverter x:Key="PercentConverter" />
</Application.Resources>
</Application>


3. now you can use it when binding to the data.

<sdk:DataGridTextColumn Header="Consumer %" IsReadOnly="True" CanUserSort="False" CanUserReorder="False" Binding="{Binding ConumserPercent,Mode=OneWay, Converter={StaticResource PercentConverter}}" />


COOL!!! Is it?
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐