WPF 之Converter

WPF  Converter

                                                                            Leo

在我们做项目的时候,经常会遇见这样的事情:

在数据中我们定义的是truefalse 而在现实的时候则可能要求男,女

我们还得能定义成了0123456

而在显示的时候确要求我们显示成了周日,周一….周六

 

那么我们究竟应该怎样做来进行转换呢?方法很多,在只有truefalse的时候直接写

If(gender)

Else

 

那么想012,。。。借助这个思想就是

Switch(week)

{

         Case 1:

                   周一;

         Break;

         Case 2:

                   ……..

         Case 0:

周日;

         Break;

}

 

在后台这样转换时可以了,可是在WPF中都是进行绑定的啊,了,我们还得封装一个实体,然后先转换了在绑定,如果这么做的话就会有些ugly…..头痛

 

还不错:Converter可以帮我们解决这样的问题:

转换的思想不便,只是有了Converter我们只要直接集成IConverter,在前台就可以进行绑定了。

如下所示:

[ValueConversion(typeof(int), typeof(String))]

    public class WeekConverter : IValueConverter

    {

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

        {

            int index = (int)value;

            string result = null;

            switch (index)

            {

                case 1:

                    result = "星期一";

                    break;

                case 2:

                    result = "星期二";

                    break;

                case 3:

                    result = "星期三";

                    break;

                case 4:

                    result = "星期四";

                    break;

                case 5:

                    result = "星期五";

                    break;

                case 6:

                    result = "星期六";

                    break;

                case 0:

                    result = "星期日";

                    break;

              

            }

            return result;

        }

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

        {

            string index = (string)value;

            int result = -1;

            switch (index)

            {

                case "周一":

                    result = 1;

                    break;

                case "周二":

                    result = 2;

                    break;

            }

            return result;

        }

    }

<Window x:Class="WorkTimeManager.Test"

                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 

                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"     

                xmlns:local="clr-namespace:WorkTimeManager"     

                Title="MainWindow" Height="350" Width="525" x:Name="mainWindow">

    <Window.Resources>

        <local:WeekConverter  x:Key="weekConvert"/>

    </Window.Resources>

    <Grid>

        <ListView x:Name="lstBox" ItemsSource="{Binding ElementName=Test,Path=ListData}">

            <ListView.ItemTemplate>

                <DataTemplate>

                    <Border Width="300" Height="30">

                        <StackPanel Orientation="Horizontal">

                            <TextBlock FontSize="12" Text="今天是:" />

                            <TextBlock FontSize="12" Text="{Binding Converter={StaticResource weekConvert}}"/>

                            <TextBlock FontSize="12" Text="数据库中的值是:"/>

                            <TextBlock FontSize="12" Text="{Binding}"/>

                        </StackPanel>

                    </Border>

                </DataTemplate>

            </ListView.ItemTemplate>

        </ListView>

    </Grid>

</Window>

在后台对数据进行一下绑定或者设置成属性都可以的!

 

总之,方便多了!

 

 

 

===========================================================

 

 

 北京]北京-梅子(771432311)  19:15:20
数据库中存储的是数字 想在页面上显示 0=> 合格 要如何实现?谁帮忙提示提示   
空空空(410502460)  19:17:16
VALUECONVERTER
[甘肃]轩辕(904417059)  19:17:24
估计link可以
提出来单独处理一下
Rolk(383628918)  19:18:01
Converter 
 
 
 大神 我看好你哟 
奋斗(283309167)  19:16:41
public class UserStatusConverter : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null)
            {
                return (((int)value) == 1 ? "启用" : "停用");
            }
            else
            {
                return DependencyProperty.UnsetValue;
            }
            
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (targetType != typeof(int)) throw new InvalidOperationException("转换的值必须是字符!");
            return (value.ToString() == "启用" ? 1 : 0);
        }
    }
别大神大神的,我是菜鸟
<telerik:GridViewDataColumn Header="用户状态"
Width="80"
DataMemberBinding="{Binding Use_Status,Converter={StaticResource UserStatusConverter},Mode=OneWay}" />
这如果还不明白,我就无语了
奋斗(283309167)  19:18:10
其实你也可以弄个参照数据库表,根据你0,对照为合格也可以做到
旅人   滴答滴(771432311)  19:18:57
我正努力看懂。。。非常感谢您的提示 。。。 
posted @ 2013-11-21 22:06  meimao5211  阅读(629)  评论(0编辑  收藏  举报