赞
踩
需求中经常有这样的问题,我输入的是一种数据类型,但是我在界面显示的完全不同的东西。这样就需要转换。
比如界面的listbox绑定的数据源为Student类,需要将性别显示出来
Student
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace WpfApp.Model { public class Student { public string ID { get; set; } public string Name { get; set; } public string Sex { get; set; } } }
ViewModel
using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; using WpfApp.Model; namespace WpfApp.ViewModel { public class MainWindow { private List<Student> students = new List<Student>(); public MainWindow() { Students = new List<Student>() { new Student() { ID = "1", Name = "Peter", Sex = "0" }, new Student() { ID = "2", Name = "Tom", Sex = "1" }, new Student() { ID = "3", Name = "Ben", Sex = "0" } }; } public List<Student> Students { get => students; set => students = value; } } }
数据转换
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Data; namespace WpfApp.Convert { public class SexConverter : IValueConverter { public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { string result = ""; string sex = (string)value; if (sex == "0") { result = "男"; } else if (sex == "1") { result = "女"; } return result; } public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new NotImplementedException(); } } }
xaml
<Window x:Class="WpfApp.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:converts="clr-namespace:WpfApp.Convert" xmlns:local="clr-namespace:WpfApp" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Window.Resources> <converts:SexConverter x:Key="convert"/> </Window.Resources> <StackPanel> <ListBox x:Name="test" ItemsSource="{Binding Students}"> <ListBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding Id}"/> <TextBlock Text="{Binding Name}"/> <TextBlock Text="{Binding Sex, Converter={StaticResource convert }}"/> </StackPanel> </DataTemplate> </ListBox.ItemTemplate> </ListBox> </StackPanel> </Window>
设置主窗体的datacontext
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new WpfApp.ViewModel.MainWindow();
}
}
也可以这么做
<Window x:Class="WpfApp1.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:converts="clr-namespace:WpfApp.Convert" xmlns:local="clr-namespace:WpfApp" Title="MainWindow" Height="450" Width="800"> <Window.Resources> <!--引用资源--> <converts:SexConverter x:Key="SexConverter"/> <Style TargetType="ListBoxItem"> <Setter Property="ContentTemplate"> <Setter.Value> <DataTemplate> <StackPanel Orientation="Horizontal"> <TextBlock Text="{Binding ID}" Width="60"/> <TextBlock Text="{Binding Name}" Width="120"/> <TextBlock Text="{Binding Sex,Converter={StaticResource SexConverter}}" Width="60"/> </StackPanel> </DataTemplate> </Setter.Value> </Setter> </Style> </Window.Resources> <StackPanel> <ListBox x:Name="listBoxStudent" Margin="5,5,4.6,5" Height="188"/> </StackPanel> </Window>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。