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

  • 相关阅读:
    bash:加减乘除(bc、let)
    shell配置文件
    HDU4624 Endless Spin 【最大最小反演】【期望DP】
    SPOJ10707 COT2
    HDU5977 Garden of Eden 【FMT】【树形DP】
    Codeforces1023E Down or Right 【贪心】
    Codeforces1023F Mobile Phone Network 【并查集】【最小生成树】
    UOJ272 [清华集训2016] 石家庄的工人阶级队伍比较坚强 【分治乘法】
    LOJ2721 [NOI2018] 屠龙勇士 【扩展中国剩余定理】
    UOJ268 [清华集训2016] 数据交互 【动态DP】【堆】【树链剖分】【线段树】
  • 原文地址:https://www.cnblogs.com/luluping/p/2039486.html
Copyright © 2011-2022 走看看