Template,ItemsPanel,ItemContainerStyle ,ItemTemplate四个属性辨析

1 篇文章 0 订阅

Template,ItemsPanel,ItemContainerStyle ,ItemTemplate四个属性辨析

ItemsControl,ListBox  等带有子项的控件四个重要属性解析;

  1. Template 为控件模板,整个控件的外观,比如加入边框,边距,背景等修改;
  2. ItemsPanel 是子项的容器,可以是Stackpanel ,Wrappanel,VirtualStackPanel等, 可以控制子项的排列方式;
  3. ItemContainerStyle 子项的布局,显示与隐藏,背景;
  4. ItemTemplate:子项的数据模板,不能控制子项的显示和隐藏; 在做过滤的功能时候需要使用ItemContainerStyle ;如果ItemContainerStyle定义了Template属性,则ItemTemplate定义的无效;

例子代码:

<Window x:Class="WpfApp1.ListBoxTest"
        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:WpfApp1"
         xmlns:s="clr-namespace:System;assembly=mscorlib"
        mc:Ignorable="d"
        Title="ListBoxTest" Height="450" Width="800" >
    <Grid>
        <ListBox HorizontalAlignment="Left"   VerticalAlignment="Top" Width="419"
                 Background="Green"  x:Name="lbPeson"            >
            <ListBox.Template>
                <ControlTemplate TargetType="{x:Type ListBox}">
                    <Border BorderBrush="Gray" BorderThickness="5" Margin="10" Background="Beige">
                        <ItemsPresenter></ItemsPresenter>
                    </Border>
                </ControlTemplate>
            </ListBox.Template>
            <ListBox.ItemsPanel>
                <ItemsPanelTemplate>
                    <StackPanel  Margin="10" Orientation="Vertical"/>
                </ItemsPanelTemplate>
            </ListBox.ItemsPanel>
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Border BorderBrush="Black" BorderThickness="0,0,0,1" Margin="10"   HorizontalAlignment="Stretch"
                            Width="200" Background="BlanchedAlmond">
                        <StackPanel Orientation="Horizontal">
                            <TextBlock Text="{Binding Path=FirstName}"></TextBlock>
                            <TextBlock Text="{Binding Path=SecondName}"></TextBlock>
                        </StackPanel>
                    </Border>
                </DataTemplate>
            </ListBox.ItemTemplate>
            <ListBox.ItemContainerStyle>
                <Style TargetType="ListBoxItem">
                    <Setter Property="HorizontalAlignment" Value="Stretch"></Setter>
                    <Setter Property="Template" >
                        <Setter.Value>
                            <ControlTemplate TargetType="ListBoxItem">
                                <Border BorderBrush="Black" BorderThickness="0,0,0,1" Margin="10" Background="AliceBlue" >
                                    <StackPanel Orientation="Vertical">
                                        <TextBlock Text="{Binding Path=FirstName}"></TextBlock>
                                        <TextBlock Text="{Binding Path=SecondName}"></TextBlock>
                                    </StackPanel>
                                </Border>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </ListBox.ItemContainerStyle>
        </ListBox>
    </Grid>
</Window>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfApp1
{
    /// <summary>
    /// ListBoxTest.xaml 的交互逻辑
    /// </summary>
    public partial class ListBoxTest : Window
    {
        public List<Person> PersonList = new List<Person>();
        public ListBoxTest()
        {
            InitializeComponent();
            for(int i=0;i<10;i++)
            {
                PersonList.Add(new Person() { FirstName = "fsn" + i, SecondName = "scd" + i });
            }
            this.lbPeson.ItemsSource = PersonList;
        }
      
    }
    public class Person
    { 
        public string FirstName { get; set; }
        public string SecondName { get; set; }
    }

}

效果图:

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值