有时我们展现的数据,需要进行转换,比如如果一个学生的成绩过了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 |
} |
运行结果
有时我们展现的数据,需要进行转换,比如如果一个学生的成绩过了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 |
} |
运行结果