App
-----------------------------------------------------------------
<Application x:Class="WPFDemo.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WPFDemo"
StartupUri="MainWindow.xaml">
<Application.Resources>
<local:TempConverter x:Key="myTempConverter"/>
<DataTemplate x:Key="temp1">
<StackPanel Orientation="Horizontal">
<TextBlock Text="temp1" Margin="0 0 10 0"/>
<TextBlock Text="{Binding Name}"/>
<Label Background="Red">this is template 1</Label>
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="temp2">
<StackPanel Orientation="Horizontal">
<TextBlock Text="temp2" Margin="0 0 10 0"/>
<TextBlock Text="{Binding Name}"/>
<Label Background="Black">this is template 1000</Label>
</StackPanel>
</DataTemplate>
</Application.Resources>
</Application>
mainwindow
----------------------------------------------------------
<Window x:Class="WPFDemo.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:local="clr-namespace:WPFDemo"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
<StackPanel>
<ListBox ItemsSource="{Binding pList}" ItemTemplate="{Binding pList,Converter={StaticResource myTempConverter}}" Height="400" x:Name="listBox1" Width="300" />
</StackPanel>
</Window>
viewModel
-------------------------------------------------------------------------------
public class MainViewModel
{
public MainViewModel()
{
_pList = new ObservableCollection<People>();
_pList.Add(new People { IsTemp1 = false, Name = "zhangsan" });
_pList.Add(new People { IsTemp1 = true, Name = "zhangsan" });
_pList.Add(new People { IsTemp1 = false, Name = "lisi" });
_pList.Add(new People { IsTemp1 = false, Name = "wangwu" });
}
private ObservableCollection<People> _pList = null;
public ObservableCollection<People> pList
{ get
{
return _pList;
}
} }
model
---------------------------------------------------------------------------------
public class People
{
public bool IsTemp1 { get; set; }
public string Name { get; set; }
}
转换类
--------------------------------------------------------
public class TempConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Collection<People> datalist = (Collection<People>)value;
DataTemplate template = new DataTemplate();
if (datalist[0].IsTemp1 == true)
{
template = App.Current.Resources["temp1"] as DataTemplate;
}
else
{
template = App.Current.Resources["temp2"] as DataTemplate;
}
return template;
} public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
标签:object,public,template,使用,IsTemp1,new,dataTemplate,pList
From: https://blog.51cto.com/u_4018548/6420080