Browse Source

added support for add items and remove items in virtualization sample app.

pull/554/head
danwalmsley 10 years ago
parent
commit
35fe93c9c2
  1. 3
      samples/VirtualizationTest/MainWindow.xaml
  2. 37
      samples/VirtualizationTest/ViewModels/MainWindowViewModel.cs

3
samples/VirtualizationTest/MainWindow.xaml

@ -21,10 +21,11 @@
UseFloatingWatermark="True"
Text="{Binding NewItemString}"/>
<Button Command="{Binding NewItemCommand}">Insert Item</Button>
<Button Command="{Binding RemoveItemCommand}">Remove Item</Button>
<Button Command="{Binding RecreateCommand}">Recreate</Button>
</StackPanel>
<ListBox Name="listBox" Items="{Binding Items}">
<ListBox Name="listBox" Items="{Binding Items}" SelectedItem="{Binding SelectedItem}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Header}"/>

37
samples/VirtualizationTest/ViewModels/MainWindowViewModel.cs

@ -10,7 +10,9 @@ namespace VirtualizationTest.ViewModels
internal class MainWindowViewModel : ReactiveObject
{
private int _itemCount = 200;
private string _newItemString;
private IReactiveList<ItemViewModel> _items;
private ItemViewModel _selectedItem;
private string _prefix = "Item";
public MainWindowViewModel()
@ -18,6 +20,18 @@ namespace VirtualizationTest.ViewModels
this.WhenAnyValue(x => x.ItemCount).Subscribe(ResizeItems);
RecreateCommand = ReactiveCommand.Create();
RecreateCommand.Subscribe(_ => Recreate());
NewItemCommand = ReactiveCommand.Create();
NewItemCommand.Subscribe(_ => Create());
RemoveItemCommand = ReactiveCommand.Create();
RemoveItemCommand.Subscribe(_ => Remove());
}
public string NewItemString
{
get { return _newItemString; }
set { this.RaiseAndSetIfChanged(ref _newItemString, value); }
}
public int ItemCount
@ -26,14 +40,24 @@ namespace VirtualizationTest.ViewModels
set { this.RaiseAndSetIfChanged(ref _itemCount, value); }
}
public ItemViewModel SelectedItem
{
get { return _selectedItem; }
set { this.RaiseAndSetIfChanged(ref _selectedItem, value); }
}
public IReactiveList<ItemViewModel> Items
{
get { return _items; }
private set { this.RaiseAndSetIfChanged(ref _items, value); }
}
public ReactiveCommand<object> NewItemCommand { get; private set; }
public ReactiveCommand<object> RecreateCommand { get; private set; }
public ReactiveCommand<object> RemoveItemCommand { get; private set; }
private void ResizeItems(int count)
{
if (Items == null)
@ -54,6 +78,19 @@ namespace VirtualizationTest.ViewModels
}
}
private void Create()
{
Items.Add(new ItemViewModel(Items.Count, NewItemString));
}
private void Remove()
{
if (SelectedItem != null)
{
Items.Remove(SelectedItem);
}
}
private void Recreate()
{
_prefix = _prefix == "Item" ? "Recreated" : "Item";

Loading…
Cancel
Save