zoukankan      html  css  js  c++  java
  • WPF 格式化输出- IValueConverter接口的使用 datagrid列中的值转换显示

    在WPF应用程序中,比如数据库表Teacher中有一个字段 Title ,int 类型,取值为1-5,分别对应讲师  研究员 高工  副教授 教授 。在wpf窗口中需要直接显示 讲师 研究员 类似的文字,而不是一个1-5的数字,从界面设计的角度 ,用文字来表示职称是最自然的方式。因此在数据绑定的时候需要将整数转换为对应的文本职称。 但是WPF没有象ASP。NET那样的绑定表达式用法;该怎么办呢?

    WPF框架提供了一个IValueConverter接口,负责数据源到数据控件绑定时,自定义转换逻辑。使用它包括以下步骤:

    1、添加引用,using System.Windows.Data;

    2、创建一个只转换器类,实现该接口;

    3、给该类加入ValueConversion属性,并制定源类型和目标类型;

    4、实现该接口的两个方法,Convert()与ConvertBack()

    5、按照1-3创建类之后,在数据绑定控件中,设定Converter为该类的一个实例;

    具体要完成上述功能:

    1.首先创建实现 IValueConverter的类:

    //指定转换器源类型和目标类型

     [ValueConversion(typeof(int), typeof(string))]
       public class TitleConverter:IValueConverter
        {

          

           //实现接口的两个方法
            #region IValueConverter 成员

            public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                int titleInt = (int)value;
                string titleStr = string.Empty;
                switch (titleInt)
                {
                    case 5:
                        titleStr = "教授";
                        break;
                    case  4:
                        titleStr = "副教授";
                        break;
                    case 3:
                        titleStr = "高工";
                        break;
                    case 2:
                        titleStr = "研究员";
                        break;
                    case 1:
                        titleStr = "讲师";
                        break;
               
                }


                return titleStr;
            }

            public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
                string titleStr = value.ToString();
                int titleInt = 0;
                switch (titleStr)
                {
                    case "教授":
                        titleInt = 5;
                        break;
                    case "副教授":
                        titleInt = 4;
                        break;
                    case "高工":
                        titleInt = 3;
                        break;
                    case "研究员":
                        titleInt = 2;
                        break;
                    case "讲师":
                        titleInt = 1;
                        break;
               
               
                }

                return titleInt;
            }

            #endregion
        }

    2.前台调用

    首先需要在窗口中声明转换器类名称空间 并定义一个转换器资源

    <Window x:Class="EvalStarServer.UserManage"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:EvalStarServer.BLL"
            Title="UserManage" >
        <Window.Resources>
            <local:TitleConverter x:Key="myTitleConverter"></local:TitleConverter>
        </Window.Resources>

    如果是在控件中,应该放在<UserControl.Resources>的<ResourceDictionary>中

    <UserControl.Resources>
            <ResourceDictionary>
                <local:TitleConverter x:Key="myTitleConverter"></local:TitleConverter>
            </ResourceDictionary>
    </UserControl.Resources>

    <DataGrid Name="gdEvaluator" CanUserAddRows="False">
                    <DataGrid.Columns>
                        <DataGridTextColumn Header="姓名" Binding="{Binding Path=Name}"></DataGridTextColumn>                 
                        <DataGridTextColumn Header="职称" Binding="{Binding Path=Elevel,Converter={StaticResource myTitleConverter}}"></DataGridTextColumn>
                        <DataGridTextColumn Header="编号" Binding="{Binding Path=Eno}"></DataGridTextColumn>
                        <DataGridTemplateColumn Header="操作">
                            <DataGridTemplateColumn.CellTemplate>
                                <DataTemplate>
                                    <Button Tag="{Binding Path=ID}"  Click="OnDeleteEvl">删除</Button>
                                </DataTemplate>
                            </DataGridTemplateColumn.CellTemplate>
                        </DataGridTemplateColumn>
                    </DataGrid.Columns>
                </DataGrid>

    </Windows>

    这样就实现了前台根据数据中的整数 显示不同的职称。

  • 相关阅读:
    Bit Calculation
    Create ArcGIS maps in Power BI Desktop
    Power Apps visual for Power BI
    Create Power BI visuals by using Python
    运行 R 脚本
    Introduction to ASP.NET Core Blazor
    Tips and tricks for color formatting in Power BI
    Sharepoint Create, Update, and Delete List Items
    Power BI Office Online Server
    jQuery Organization Chart
  • 原文地址:https://www.cnblogs.com/hanzq/p/12335109.html
Copyright © 2011-2022 走看看