silverlight 自定义转换器

 

 

public class SexConverter : System.Windows.Data.IValueConverter

    {

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

        {

            //if (targetType != typeof(string)) throw new InvalidOperationException("The target must be a integer!");

            if (value != null)

                return (value.ToString().ToLower() == "false" ? "女" : "男");

            else

                return "";

        }

 

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)

        {

            //if (targetType != typeof(Int32)) throw new InvalidOperationException("The target must be a String!");

            return (value.ToString() == "女" ? 0 : 1);

        }

    }

 

 

 

 

<UserControl x:Class="YY.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"

    mc:Ignorable="d"

    xmlns:local="clr-namespace:YY"    

 

    d:DesignHeight="400" d:DesignWidth="600" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk">

 

    <UserControl.Resources>

        <local:SexConverter x:Key="sexConvert"/>

    </UserControl.Resources>

 

    <Grid x:Name="LayoutRoot" Background="White">

        <sdk:DataGrid AutoGenerateColumns="False" IsReadOnly="True" Height="297" HorizontalAlignment="Left" Margin="10,10,0,0" Name="daDisplay" VerticalAlignment="Top" Width="578" RowEditEnded="daDisplay_RowEditEnded">

            <sdk:DataGrid.Columns>

                <sdk:DataGridTextColumn Binding="{Binding UserName,Mode=TwoWay}" Header="用户名" Width="200"/>

                <sdk:DataGridTextColumn Binding="{Binding Mode=OneWay, Path=RegDate, StringFormat=/{0:d/}}" Header="注册时间" Width="100"/>

                <sdk:DataGridTextColumn Binding="{Binding Activation,Converter={ StaticResource sexConvert}}" Header="Test" CanUserSort="True" CanUserResize="False" />

            </sdk:DataGrid.Columns>

        </sdk:DataGrid>

 

        <Button Content="删除" Height="23" HorizontalAlignment="Left" Click="btDel_Click" Margin="86,323,0,0" Name="btDel" VerticalAlignment="Top" Width="75" />

        <Button Content="添加" Height="23" HorizontalAlignment="Left" Click="btAdd_Click"  Margin="178,323,0,0" Name="btAdd" VerticalAlignment="Top" Width="75" />

    </Grid>

</UserControl>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值