zoukankan      html  css  js  c++  java
  • WPF数据转换

    有时我们展现的数据,需要进行转换,比如如果一个学生的成绩过了60,我们显示一个Pass的图片。

    XAML:

    01 <Window x:Class="DeepXAML.MainWindow"
    03         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    04         xmlns:local="clr-namespace:DeepXAML"       
    05         xmlns:sys="clr-namespace:System;assembly=mscorlib"
    06         Title="MainWindow" Height="250" Width="450">
    07     <Window.Resources>
    08         <local:ScoreToImageConverter x:Key="sti"></local:ScoreToImageConverter>
    09     </Window.Resources>
    10     <StackPanel>
    11         <ListBox x:Name="listBoxStudents" Margin="10">
    12             <ListBox.ItemTemplate>
    13                 <DataTemplate>
    14                     <StackPanel Orientation="Horizontal">
    15                         <TextBlock Text="{Binding Path=Name}"></TextBlock>                        
    16                         <Image Source="{Binding Path=Score,Converter={StaticResource sti}}" Height="20"></Image>
    17                     </StackPanel>
    18                 </DataTemplate>
    19             </ListBox.ItemTemplate>
    20         </ListBox>
    21     </StackPanel>
    22 </Window>

    后台代码:

    01 using System;
    02 using System.Collections.Generic;
    03 using System.Windows;
    04 using System.Windows.Data;
    05 using System.Windows.Documents;
    06   
    07 namespace DeepXAML
    08 {
    09     public partial class MainWindow : Window
    10     {
    11         public MainWindow()
    12         {
    13             InitializeComponent();
    14             List<Student> students = new List<Student>{
    15              new Student{ Name="Jack", Score=90},
    16               new Student{Name="Tom", Score=30},
    17               new Student{ Name="David", Score=80}
    18             };
    19             this.listBoxStudents.ItemsSource = students;
    20         }
    21   
    22     }
    23     public class Student
    24     {
    25         public string Name { get; set; }
    26         public double Score { get; set; }
    27     }
    28     public class ScoreToImageConverter : IValueConverter
    29     {
    30         public object Convert(object value, Type targetType, object parameter,
    31             System.Globalization.CultureInfo culture)
    32         {
    33             double score = (double)value;
    34   
    35             return score >= 60 ? @"\images\pass.gif" : @"\images\nopass.gif";
    36         }
    37   
    38         public object ConvertBack(object value, Type targetType, object parameter,
    39             System.Globalization.CultureInfo culture)
    40         {
    41             throw new NotImplementedException();
    42         }
    43     }
    44 }

    运行结果

    image

  • 相关阅读:
    A survey for developing of reinforcement learning environments
    04-STM32+ESP8266+AIR202基本控制篇-功能2-微信小程序使用APUConfig配网绑定ESP8266,并通过MQTT和ESP8266实现远程通信控制
    03-STM32+ESP8266+AIR202基本控制篇-功能1-APP使用APUConfig配网绑定ESP8266,并通过MQTT和ESP8266实现远程通信控制
    02-STM32+ESP8266+AIR202基本控制篇- 基本控制篇概述
    01-STM32+ESP8266+AIR202基本控制篇-硬件使用说明
    深入学习Redis(3):主从复制(转)
    k8s常用命令
    photoshop,3dsmax,maya等图形图像动画学习资料收集
    工业机器人(ABB+Kuka)资料打包下载
    .DS_Store 是什么
  • 原文地址:https://www.cnblogs.com/luluping/p/2039486.html
Copyright © 2011-2022 走看看