Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 20th, 2012  |  syntax: None  |  size: 4.98 KB  |  hits: 8  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Dynamically Add and Remove stack panel
  2. <ItemsControl ItemsSource="{Binding DpData}">
  3.     <ItemsControl.ItemsPanel>
  4.         <ItemsPanelTemplate>
  5.             <WrapPanel />
  6.         </ItemsPanelTemplate>
  7.     </ItemsControl.ItemsPanel>
  8.     <ItemsControl.ItemTemplate>
  9.         <DataTemplate>
  10.             <Grid>
  11.                 <!-- The content goes here, X-Button will be overlayed -->
  12.                 <Button HorizontalAlignment="Right"
  13.                         VerticalAlignment="Top"
  14.                         Content="X" Click="RemoveItem_Click"/>
  15.             </Grid>
  16.         </DataTemplate>
  17.     </ItemsControl.ItemTemplate>
  18. </ItemsControl>
  19.        
  20. // Does not need to be a dependency property.
  21. public static readonly DependencyProperty DpDataProperty =
  22.     DependencyProperty.Register("DpData", typeof(ObservableCollection<Employee>), typeof(MainWindow), new UIPropertyMetadata(new ObservableCollection<Employee>()));
  23. public ObservableCollection<Employee> DpData
  24. {
  25.     get { return (ObservableCollection<Employee>)GetValue(DpDataProperty); }
  26.     set { SetValue(DpDataProperty, value); }
  27. }
  28.        
  29. // For simplicity i use an event instead of a command.
  30. private void RemoveItem_Click(object sender, RoutedEventArgs e)
  31. {
  32.     var emp = (sender as FrameworkElement).DataContext as Employee;
  33.     DpData.Remove(emp);
  34. }
  35.        
  36. <Window.Resources>
  37.     <DataTemplate x:Key="itemsTemplate">
  38.         <!-- This might also be included in a UserControl -->
  39.         <Border Width="200" Height="200" CornerRadius="10,10,10,10" Margin="4,4,0,0" Padding="4,4,4,4">
  40.             <Border.Background>
  41.                 <LinearGradientBrush EndPoint="1,0.5" StartPoint="0,0.5">
  42.                     <GradientStop Color="AliceBlue" Offset="0"/>
  43.                     <GradientStop Color="Bisque" Offset="1"/>
  44.                 </LinearGradientBrush>
  45.             </Border.Background>
  46.             <Grid >
  47.                 <Button HorizontalAlignment="Right" VerticalAlignment="Top" Width="30" Height="20"  Content="X" Background="Red" Click="Remove_Click"></Button>
  48.                 <WrapPanel Orientation="Vertical">
  49.                 <TextBox Text="{Binding Name}" Margin="10,10" Height="20"  ></TextBox>
  50.                     <TextBox Text="{Binding City}" Margin="10,10" Height="20" ></TextBox>
  51.                     <TextBox Text="{Binding Age}" Margin="10,10" Height="20"></TextBox>
  52.                     <TextBox Text="{Binding Count}" Margin="10,10" Height="20" ></TextBox>
  53.                 </WrapPanel>
  54.             </Grid>
  55.         </Border>
  56.     </DataTemplate>
  57. </Window.Resources>
  58. <Grid>
  59.     <ItemsControl
  60.  
  61.               Width="Auto"
  62.               Height="Auto"
  63.               ItemsSource="{Binding ElementName=mainWindow, Path=EmployeeCollection}"
  64.               ItemTemplate="{StaticResource itemsTemplate}"  Background="Aqua">
  65.         <ItemsControl.ItemsPanel>
  66.             <ItemsPanelTemplate>
  67.                 <WrapPanel Orientation="Vertical" />
  68.             </ItemsPanelTemplate>
  69.         </ItemsControl.ItemsPanel>
  70.     </ItemsControl>
  71.     <Button Content="Add" Height="23" HorizontalAlignment="Left" Margin="487,287,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="Add_Click" />
  72. </Grid>
  73.        
  74. using System;
  75. using System.Collections.Generic;
  76. using System.Linq;
  77. using System.Text;
  78. using System.Windows;
  79. using System.Windows.Controls;
  80. using System.Windows.Data;
  81. using System.Windows.Documents;
  82. using System.Windows.Input;
  83. using System.Windows.Media;
  84. using System.Windows.Media.Imaging;
  85. using System.Windows.Navigation;
  86. using System.Windows.Shapes;
  87. using System.Collections.ObjectModel;
  88.  
  89. namespace DynamicAddRemovePanel
  90. {
  91.     /// <summary>
  92.     /// Interaction logic for MainWindow.xaml
  93.     /// </summary>
  94.     public partial class MainWindow : Window
  95.     {
  96.         public ObservableCollection<Employee> employeeCollection = new ObservableCollection<Employee>();
  97.  
  98.         public ObservableCollection<Employee> EmployeeCollection
  99.         {
  100.             get
  101.             {
  102.                 return this.employeeCollection;
  103.             }
  104.             set
  105.             {
  106.                 this.employeeCollection = value;
  107.             }
  108.         }
  109.         public MainWindow()
  110.         {
  111.             InitializeComponent();
  112.         }
  113.  
  114.  
  115.  
  116.  
  117.         public class Employee
  118.         {
  119.             public string Name { get; set; }
  120.             public string City { get; set; }
  121.             public int Age { get; set; }
  122.             public static int Count { get; set; }
  123.             public Employee()
  124.             {
  125.                 Count++;
  126.             }
  127.             public Employee(string nameArg, string cityArg, int ageArg)
  128.             {
  129.                 this.Name = nameArg;
  130.                 this.City = cityArg;
  131.                 this.Age = ageArg;
  132.                 Count++;
  133.            }
  134.         }
  135.  
  136.         private void Add_Click(object sender, RoutedEventArgs e)
  137.         {
  138.             employeeCollection.Add(new Employee("Pritesh","Abad",22));
  139.         }
  140.         private void Remove_Click(object sender, RoutedEventArgs e)
  141.         {
  142.             var emp = (sender as FrameworkElement).DataContext as Employee;
  143.             employeeCollection.Remove(emp);
  144.         }
  145.  
  146.  
  147.     }
  148. }