Collection editor improvements. Allows reordering of elements, and actually updates the list when content changes.

This commit is contained in:
Brant Martin
2018-07-23 21:50:42 -04:00
parent 3b72724966
commit 14e16a959f
2 changed files with 51 additions and 7 deletions

View File

@@ -8,6 +8,7 @@
d:DesignHeight="300" d:DesignWidth="300">
<Grid Width="Auto" Height="Auto">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
@@ -15,17 +16,23 @@
<RowDefinition />
<RowDefinition Height="25" />
</Grid.RowDefinitions>
<ListBox Grid.Row="0" Grid.Column="0" x:Name="ElementList"
<StackPanel Grid.Column="0" Grid.Row="0" VerticalAlignment="Center" Width="20">
<Button Content="⭱" FontSize="22" Click="TopButton_Click"/>
<Button Content="⭡" FontSize="22" Click="UpButton_Click"/>
<Button Content="⭣" FontSize="22" Click="DownButton_Click"/>
<Button Content="⭳" FontSize="22" Click="BottomButton_Click"/>
</StackPanel>
<ListBox Grid.Row="0" Grid.Column="1" x:Name="ElementList"
HorizontalContentAlignment="Stretch" Margin="0" VerticalContentAlignment="Stretch" />
<GridSplitter Grid.Column="1" Grid.Row="0" Width="2" HorizontalAlignment="Left" VerticalAlignment="Stretch"
<GridSplitter Grid.Column="2" Grid.Row="0" Width="2" HorizontalAlignment="Left" VerticalAlignment="Stretch"
Background="Gray" ShowsPreview="True" VerticalContentAlignment="Stretch" />
<local:PropertyGrid Grid.Row="0" Grid.Column="1" x:Name="PGrid" Margin="4,0,0,0" />
<Button Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" x:Name="AddButton" Content="Add"
HorizontalAlignment="Left" Margin="0" VerticalAlignment="Top"
<local:PropertyGrid Grid.Row="0" Grid.Column="2" x:Name="PGrid" Margin="4,0,0,0" />
<Button Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="3" x:Name="AddButton" Content="Add"
HorizontalAlignment="Left" Margin="0" VerticalAlignment="Top" ClipToBounds="False"
Width="90" />
<Button Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="2" x:Name="RemoveButton" Content="Remove"
HorizontalAlignment="Left" Margin="100,0,0,0"
<Button Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="3" x:Name="RemoveButton" Content="Remove"
HorizontalAlignment="Left" Margin="100,0,0,0" ClipToBounds="False"
VerticalAlignment="Top" Width="90" />
</Grid>

View File

@@ -2,6 +2,7 @@
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
@@ -78,12 +79,15 @@ namespace Torch.Views
public void Edit<T>(ICollection<T> collection) where T : new()
{
var oc = collection as ObservableCollection<T> ?? new ObservableCollection<T>(collection);
bool not = typeof(INotifyPropertyChanged).IsAssignableFrom(typeof(T));
AddButton.Click += (sender, args) =>
{
var t = new T();
oc.Add(t);
ElementList.SelectedItem = t;
if (not)
((INotifyPropertyChanged)t).PropertyChanged += (o, eventArgs) => RefreshList();
};
RemoveButton.Click += RemoveButton_OnClick<T>;
@@ -91,6 +95,11 @@ namespace Torch.Views
ElementList.ItemsSource = oc;
oc.CollectionChanged += (sender, args) => RefreshList();
if (not)
{
foreach(var t in oc)
((INotifyPropertyChanged)t).PropertyChanged += (o, eventArgs) => RefreshList();
}
if (!(collection is ObservableCollection<T>))
{
@@ -121,6 +130,34 @@ namespace Torch.Views
{
ElementList.Items.Refresh();
}
private void TopButton_Click(object sender, RoutedEventArgs e)
{
var i = ElementList.SelectedItems[0];
ElementList.Items.Remove(i);
ElementList.Items.Insert(0, i);
}
private void BottomButton_Click(object sender, RoutedEventArgs e)
{
var i = ElementList.SelectedItems[0];
ElementList.Items.Remove(i);
ElementList.Items.Add(i);
}
private void UpButton_Click(object sender, RoutedEventArgs e)
{
var i = ElementList.SelectedItems[0];
var idx = ElementList.SelectedItems.IndexOf(i);
ElementList.Items.Remove(i);
ElementList.Items.Insert(Math.Max(0, idx), i);
}
private void DownButton_Click(object sender, RoutedEventArgs e)
{
var i = ElementList.SelectedItems[0];
var idx = ElementList.SelectedItems.IndexOf(i);
ElementList.Items.Remove(i);
var mx = ElementList.Items.Count - 1;
ElementList.Items.Insert(Math.Min(idx, mx), i);
}
}
}