ItemsControl 具有一个特殊功能,可以为控件中显示的项目选择模板。在下面的示例中,我说明了 DataTemplateSelector 的使用
首先是 XAML。在 Resource 部分中,为要在 ListBox 中显示的不同类型的项目定义了不同的数据模板,并且有对 DataTemplateSelector 的引用(稍后在 C# 代码中定义):
<Window x:Class="TestWpfApp.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:TestWpfApp"
mc:Ignorable="d"
x:Name="DataWindow"
WindowState="Maximized"
Title="MainWindow" Height="450" Width="800">
<Window.Resources>
<!-- The data template selector -->
<local:DataItemTemplateSelector x:Key="DataItemTemplateSelector"/>
<!-- Data template for a normal data item -->
<DataTemplate x:Key="NormalDatatemplate">
<Border Width="200"
Margin="0"
Background="White"
BorderThickness="2"
Padding="3"
BorderBrush="Transparent">
<TextBlock VerticalAlignment="Center"
HorizontalAlignment="Stretch"
FontSize="20"
Foreground="Black"
Background="Transparent"
Text="{Binding Path=Header}"/>
</Border>
</DataTemplate>
<!-- Data template for a special data item -->
<DataTemplate x:Key="SpecialDatatemplate">
<Border Width="200"
Margin="0"
Background="Yellow"
BorderThickness="2"
Padding="3"
BorderBrush="Blue">
<TextBlock VerticalAlignment="Center"
HorizontalAlignment="Stretch"
FontSize="20"
Foreground="Black"
Background="Transparent"
Text="{Binding Path=Header}"/>
</Border>
</DataTemplate>
</Window.Resources>
<Grid>
<ListBox x:Name="DataListBox"
Margin="50"
Width="Auto"
Height="Auto"
HorizontalAlignment="Center"
VerticalAlignment="Top"
ItemsSource="{Binding ElementName=DataWindow, Path=DataList}"
ItemTemplateSelector="{StaticResource ResourceKey=DataItemTemplateSelector}"/>
</Grid>
</Window>
然后是 C# 代码,在那里定义了 DataItemTemplateSelector 和带有一些测试数据的 DataList:
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
namespace TestWpfApp
{
// Different types of items on the list
public enum DataItemType
{
Normal,
Special
}
// Items on the list
public class DataItem
{
public string Header { set; get; }
public DataItemType DataType { set; get; }
}
// Data template selector for items on the list
public class DataItemTemplateSelector : DataTemplateSelector
{
public override DataTemplate SelectTemplate(object dataItem, DependencyObject container)
{
// Check that everyting is OK
if ((dataItem != null) && (dataItem is DataItem))
{
// Switch on the data type
switch (((DataItem)(dataItem)).DataType)
{
case DataItemType.Normal:
// Return the data template for a normal data item
return Application.Current.MainWindow.FindResource("NormalDatatemplate") as DataTemplate;
case DataItemType.Special:
// Return the data template for a special data item
return Application.Current.MainWindow.FindResource("SpecialDatatemplate") as DataTemplate;
default:
return null;
}
}
return null;
}
}
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
// The list of data to show in the list box
public List<DataItem> DataList { set; get; }
// The constructor of the main window
public MainWindow()
{
// Some test data
this.DataList = new List<DataItem>();
this.DataList.Add(new DataItem() { Header = "Item 1", DataType = DataItemType.Normal });
this.DataList.Add(new DataItem() { Header = "Item 2", DataType = DataItemType.Normal });
this.DataList.Add(new DataItem() { Header = "Item 3", DataType = DataItemType.Special });
this.DataList.Add(new DataItem() { Header = "Item 4", DataType = DataItemType.Normal });
// Initializing the window
InitializeComponent();
}
}
}
我希望您了解使用 DataTemplateSelector,然后您可以设置您的 ListBox 和其中不同类型的项目的样式