Grouping Implementation of ItemsControl in WPF, wpfitemscontrol

Source: Internet
Author: User

Grouping Implementation of ItemsControl in WPF, wpfitemscontrol

When ItemsControl is used, groups such as ListBox, ListView, and DataGrid are sometimes used. The ItemsControl of WPF can be grouped. It relies on GroupStyle. Taking ListBox as an example, its grouping is:

The foreground is as follows:

1 <ListBox Name = "lbMain"> 2 <ListBox. itemTemplate> 3 <DataTemplate> 4 <StackPanel Orientation = "Horizontal"> 5 <TextBlock Text = "{Binding FileName}" 6 Width = "150"/> 7 <TextBlock Text =" {Binding AuthorName} "8 Width =" 100 "/> 9 <TextBlock Text =" {Binding UpTime} "10 Width =" 100 "/> 11 </StackPanel> 12 </ dataTemplate> 13 </ListBox. itemTemplate> 14 <ListBox. groupStyle> 15 <GroupStyle> 16 <GroupStyle. containerStyle> 17 <Style TargetType = "{x: Type GroupItem}"> 18 <Setter Property = "Template"> 19 <Setter. value> 20 <ControlTemplate TargetType = "{x: Type GroupItem}"> 21 <Expander IsExpanded = "True" 22 ExpandDirection = "Down"> 23 <Expander. header> 24 <StackPanel Orientation = "Horizontal"> 25 <TextBlock Text = "{Binding Path = Name}" 26 VerticalAlignment = "Center"/> 27 <TextBlock Text = "{Binding path = ItemCount, stringFormat = Quantity: {0} "28 verticalignment =" Center "29 Margin =" 5, 0, 0 "/> 30 <Button Content =" Sale "31 Margin =" 5, 0, 0, 0 "/> 32 </StackPanel> 33 </Expander. header> 34 <ItemsPresenter/> 35 </Expander> 36 </ControlTemplate> 37 </Setter. value> 38 </Setter> 39 </Style> 40 </GroupStyle. containerStyle> 41 </GroupStyle> 42 </ListBox. groupStyle> 43 </ListBox>

From Lines 16, we can see that GroupStyle defines the internal style of the control, so if someone tries to bind the object data attribute here, it will certainly fail. Note that 25 rows can only be Name, no matter what the group attribute is called, it can only be a Name. I wrote a Button in it. If you want to know why it can only be a Name, write a Click, print the DataContext of the Button. If you want to do more processing here, such as performing some operations, you can write and add the converter.

Here we just get an original Expander to load the grouping control, and you need to beautify it to write another style.

The following is the background:

1 public class ModelFile 2 {3 public string FileName {get; set;} 4 public string AuthorName {get; set;} 5 public string UpTime {get; set ;} 6} 7 8 public partial class MainWindow: Window 9 {10 public ObservableCollection <ModelFile> CollectionModelFile = new ObservableCollection <ModelFile> (); 11 12 public MainWindow () 13 {14 InitializeComponent (); 15 16 CollectionModelFile. add (new ModelFile () {FileName = "WPF historical History", AuthorName = "Wang peng", UpTime = "2014-10-10"}); 17 CollectionModelFile. add (new ModelFile () {FileName = "Introduction to WPF", AuthorName = "", UpTime = "2014-10-10"}); 18 CollectionModelFile. add (new ModelFile () {FileName = "the beauty of WPF", AuthorName = "bug", UpTime = "2014-10-11"}); 19 CollectionModelFile. add (new ModelFile () {FileName = "WPF path", AuthorName = "qingcao", UpTime = "2014-11-11"}); 20 CollectionModelFile. add (new ModelFile () {FileName = "WPF Zen", AuthorName = "dether ghost", UpTime = "2014-11-11"}); 21 CollectionModelFile. add (new ModelFile () {FileName = "WPF ", AuthorName = "what to eat tonight", UpTime = "2014-11-11"}); 22 CollectionModelFile. add (new ModelFile () {FileName = "WPF magic", AuthorName = "", UpTime = "2014-12-12"}); 23 CollectionModelFile. add (new ModelFile () {FileName = "WPF is not known", AuthorName = "", UpTime = "2014-12-12"}); 24 CollectionModelFile. add (new ModelFile () {FileName = "WPF revolution", AuthorName = "two knives", UpTime = "2014-12-12"}); 25 26 lbMain. itemsSource = CollectionModelFile; 27 28 ICollectionView cv = CollectionViewSource. getDefaultView (lbMain. itemsSource); 29 cv. groupDescriptions. add (new PropertyGroupDescription ("UpTime"); 30} 31}

The focus is on lines 28 and 29. With these two sentences, The ListBox can be accurately displayed in groups, and other ItemsControl groups are similar.

Now, a simple ListBox group display is complete. The Demo has been placed in the group. You can download it as needed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.