一、ItemsControl
简介
- ItemsControl 是用来表示一些条目集合的控件,它的成员是一些其它控件的集合。
- 其继承关系如下:
- 其常用的派生控件为:
ListBox
、ListView
、ComboBox
,为ItemsControl的具体实现。 - ItemsControl的成员条目可以为不同的类型,如自定义的类型等。常常用于派生的
ListBox
、ListView
、ComboBox
等控件的子条目中。
二、ItemsControl
控件显示数据
1、设置自定义数据类型 :MyTask
// 自定义数据类型
public class MyTask
{
public int TaskPriority { set; get; }
public string TaskName { set; get; }
public string TaskDescription { set; get; }
public MyTask(int taskPriority,string taskName,string taskDescription)
{
TaskPriority = taskPriority;
TaskName = taskName;
TaskDescription = taskDescription;
}
}
// 设置绑定数据的类型
public class MyTodoList:ObservableCollection<MyTask>
{
public MyTodoList()
{
Add(new MyTask(2, "Shopping", "go to shopping today"));
Add(new MyTask(2, "Laundry", "need to Laundry" ));
Add(new MyTask(1, "Email", "Remember set a email" ));
Add(new MyTask(3,"Clean","Clean my Office" ));
Add(new MyTask(1, "Dinner", "go to dinner" ));
Add(new MyTask(2, "Proposals", "make a Proposals" ));
}
}
2、将其自定义的数据类型添加到XAML
文件的资源中:
<Window.Resources>
<ResourceDictionary>
<!--创建一个MyTodoList 类型的实例,并将其作为资源-->
<local:MyTodoList x:Key="myTodoList"/>
</ResourceDictionary>
</Window.Resources>
3、创建ItemsControl
控件,并将其ItemsSource
属性绑定到以上自定义数据,以显示上面的数据
<!--创建ItemsControl,让其条目的数值为绑定的数据源的值-->
<ItemsControl Grid.Row="1" Grid.Column="0" Margin="10" ItemsSource="{Binding Source={StaticResource myTodoList}}">
<ItemsControl.Template> <!--ItemsControl作为一个容器,设置其整体的外观-->
<ControlTemplate TargetType="ItemsControl">
<Border BorderBrush="Red" BorderThickness="6" CornerRadius="30">
<ItemsPresenter/>
</Border>
</ControlTemplate>
</ItemsControl.Template>
<ItemsControl.ItemsPanel> <!--设置该容器中子控件的布局类型为 WrapPanel-->
<ItemsPanelTemplate>
<WrapPanel/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>
<ItemsControl.ItemTemplate> <!--设置容器中每个条目的模板-->
<DataTemplate>
<DataTemplate.Resources>
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="14"/>
<Setter Property="HorizontalAlignment" Value="Center"/>
</Style>
</DataTemplate.Resources>
<Grid>
<Ellipse Fill="Silver"/>
<StackPanel>
<TextBlock Margin="3,3,3,0" Text="{Binding Path=TaskPriority}"/>
<TextBlock Margin="3,0,3,7" Text="{Binding Path=TaskName}"/>
</StackPanel>
</Grid>
</DataTemplate>
</ItemsControl.ItemTemplate>
<ItemsControl.ItemContainerStyle> <!--设置容器中每个子控件的样式,如尺寸及Triggers等-->
<Style>
<Setter Property="Control.Width" Value="100"/>
<Setter Property="Control.Margin" Value="5"/>
<Style.Triggers>
<Trigger Property="Control.IsMouseOver" Value="True">
<Setter Property="Control.ToolTip"
Value="{Binding RelativeSource={x:Static RelativeSource.Self},Path=Content.TaskDescription}"/>
</Trigger>
</Style.Triggers>
</Style>
</ItemsControl.ItemContainerStyle>
</ItemsControl>
4、运行结果如下图所示:
三、重要属性总结
1、ItemsSource:主要用来绑定到数据源,以将数据填充到ItemsControl
中。如:ItemsSource="{Binding Source={StaticResource placesData}}"
2、ItemContainerStyle:其类型为Style
,用来设置ItemsControl
对应item
的外观样式。可在资源中设置该属性,以控制每个items
的样式Style
。如:ItemContainerStyle="{StaticResource lsty}"
3、ItemsPanel:设置items
如何进行布局,如:是以StackPanel
的形式,还是以Grid
的形式来显示ItemsControl
包含的所有元素。
4、ItemTemplate:其类型为DataTemplate
,由于控件对应的条目主要就是用来显示数据的,所以其条目模板在此就是用来设置数据显示样式的,如上面的DataTemplate
设定数据的显示方式。【注意:与第二点的区别,ItemContainerStyle对应的是每个具体item
的样式style
;而第四点对应的是每个item
的模板Template
,用于自定义数据显示的样式】