silverlight 使用IValueConverter 转换

在绑定数据中 有时候我们需要转换相关数据类型 silverlight提供了一个System.Windows.Data.IValueConverter接口

它提供两个方法 Convert和ConvertBack  前者是资源到目标元素时转换  后者是目标到资源时转换

先创建一个类型

 public class DataTimeConvert : System.Windows.Data.IValueConverter
    {

        // 参数:
        //   value:
        //     正传递到源的目标数据。
        //
        //   targetType:
        //     源对象需要的数据的 System.Type。
        //
        //   parameter:
        //     要在转换器逻辑中使用的可选参数。
        //
        //   culture:
        //     转换的区域性。
        //
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return  (value).ToString();
        }
        // 参数:
        //   value:
        //     正传递到源的目标数据。
        //
        //   targetType:
        //     源对象需要的数据的 System.Type。
        //
        //   parameter:
        //     要在转换器逻辑中使用的可选参数。
        //
        //   culture:
        //     转换的区域性。
        //
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return value.ToString();
        }

然后xaml代码

 

<UserControl x:Class="TemplateConvert.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
     xmlns:convert="clr-namespace:TemplateConvert"
    mc:Ignorable="d"
    d:DesignHeight="300" d:DesignWidth="400">
    <UserControl.Resources>
        <convert:TextChange x:Key="txtChange"></convert:TextChange>
    </UserControl.Resources>
    <Grid x:Name="LayoutRoot" Background="White">
        <ListBox x:Name="lblist">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Title,Converter={StaticResource txtChange}}" x:Name="tbTest" ></TextBlock> 
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
       
        </Grid>
</UserControl>

 

 

转载于:https://www.cnblogs.com/akingyao/archive/2013/05/09/3068826.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值