Browse Source

Add "Add Item" button to ItemsRepeaterPage.

Steven Kirk 6 years ago
parent
commit
8ef5fdfbc1

+ 3 - 2
samples/ControlCatalog/Pages/ItemsRepeaterPage.xaml

@@ -6,19 +6,20 @@
       <TextBlock Classes="h1">ItemsRepeater</TextBlock>
       <TextBlock Classes="h2">A data-driven collection control that incorporates a flexible layout system, custom views, and virtualization.</TextBlock>
     </StackPanel>
-    <StackPanel DockPanel.Dock="Right" Margin="8 0">
+    <StackPanel DockPanel.Dock="Right" Margin="8 0" Spacing="4">
       <ComboBox SelectedIndex="0" SelectionChanged="LayoutChanged">
         <ComboBoxItem>Stack - Vertical</ComboBoxItem>
         <ComboBoxItem>Stack - Horizontal</ComboBoxItem>
         <ComboBoxItem>UniformGrid - Vertical</ComboBoxItem>
         <ComboBoxItem>UniformGrid - Horizontal</ComboBoxItem>
       </ComboBox>
+      <Button Command="{Binding AddItem}">Add Item</Button>
     </StackPanel>
     <Border BorderThickness="1" BorderBrush="{DynamicResource ThemeBorderMidBrush}" Margin="0 0 0 16">
       <ScrollViewer Name="scroller"
                     HorizontalScrollBarVisibility="Auto"
                     VerticalScrollBarVisibility="Auto">
-        <ItemsRepeater Name="repeater" Items="{Binding}"/>
+        <ItemsRepeater Name="repeater" Items="{Binding Items}"/>
       </ScrollViewer>
     </Border>
   </DockPanel>

+ 2 - 1
samples/ControlCatalog/Pages/ItemsRepeaterPage.xaml.cs

@@ -3,6 +3,7 @@ using Avalonia.Controls;
 using Avalonia.Controls.Primitives;
 using Avalonia.Layout;
 using Avalonia.Markup.Xaml;
+using ControlCatalog.ViewModels;
 
 namespace ControlCatalog.Pages
 {
@@ -16,7 +17,7 @@ namespace ControlCatalog.Pages
             this.InitializeComponent();
             _repeater = this.FindControl<ItemsRepeater>("repeater");
             _scroller = this.FindControl<ScrollViewer>("scroller");
-            DataContext = Enumerable.Range(1, 100000).Select(i => $"Item {i}" ).ToArray();
+            DataContext = new ItemsRepeaterPageViewModel();
         }
 
         private void InitializeComponent()

+ 24 - 0
samples/ControlCatalog/ViewModels/ItemsRepeaterPageViewModel.cs

@@ -0,0 +1,24 @@
+using System.Collections.ObjectModel;
+using System.Linq;
+using ReactiveUI;
+
+namespace ControlCatalog.ViewModels
+{
+    public class ItemsRepeaterPageViewModel : ReactiveObject
+    {
+        private int newItemIndex = 1;
+
+        public ItemsRepeaterPageViewModel()
+        {
+            Items = new ObservableCollection<string>(
+                Enumerable.Range(1, 100000).Select(i => $"Item {i}"));
+        }
+
+        public ObservableCollection<string> Items { get; }
+
+        public void AddItem()
+        {
+            Items.Insert(0, $"New Item {newItemIndex++}");
+        }
+    }
+}