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>

 

你可能感兴趣的:(datagrid,object,header,silverlight,button,binding)