Merge pull request #229 from TorchAPI/propertygrid

Propertygrid
This commit is contained in:
John Gross
2018-04-28 10:33:25 -07:00
committed by GitHub
4 changed files with 62 additions and 6 deletions

View File

@@ -271,6 +271,7 @@
<Compile Include="Views\DictionaryEditor.xaml.cs"> <Compile Include="Views\DictionaryEditor.xaml.cs">
<DependentUpon>DictionaryEditor.xaml</DependentUpon> <DependentUpon>DictionaryEditor.xaml</DependentUpon>
</Compile> </Compile>
<Compile Include="Views\DisplayAttribute.cs" />
<Compile Include="Views\ObjectCollectionEditor.xaml.cs"> <Compile Include="Views\ObjectCollectionEditor.xaml.cs">
<DependentUpon>ObjectCollectionEditor.xaml</DependentUpon> <DependentUpon>ObjectCollectionEditor.xaml</DependentUpon>
</Compile> </Compile>

View File

@@ -0,0 +1,37 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Torch.Views
{
public class DisplayAttribute : Attribute
{
public string Name;
public string Description;
public string ToolTip;
public string GroupName;
public int Order;
public bool Enabled = true;
public bool Visible = true;
public bool ReadOnly = false;
public DisplayAttribute()
{ }
public static implicit operator DisplayAttribute(System.ComponentModel.DataAnnotations.DisplayAttribute da)
{
if (da == null)
return null;
return new DisplayAttribute()
{
Name = da.GetName(),
Description = da.GetDescription(),
GroupName = da.GetGroupName(),
Order = da.GetOrder() ?? 0
};
}
}
}

View File

@@ -17,7 +17,7 @@
<ColumnDefinition/> <ColumnDefinition/>
</Grid.ColumnDefinitions> </Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Text="Filter: " Margin="3"/> <TextBlock Grid.Column="0" Text="Filter: " Margin="3"/>
<TextBox Grid.Column="1" Margin="3" TextChanged="UpdateFilter"/> <TextBox Name="TbFilter" Grid.Column="1" Margin="3" TextChanged="UpdateFilter" IsEnabled="False"/>
</Grid> </Grid>
<ScrollViewer Grid.Row="1" x:Name="ScrollViewer"/> <ScrollViewer Grid.Row="1" x:Name="ScrollViewer"/>
<TextBlock x:Name="TbDescription" Grid.Row="2" MinHeight="18"/> <TextBlock x:Name="TbDescription" Grid.Row="2" MinHeight="18"/>

View File

@@ -49,12 +49,14 @@ namespace Torch.Views
if (e.NewValue == null) if (e.NewValue == null)
{ {
ScrollViewer.Content = null; ScrollViewer.Content = null;
TbFilter.IsEnabled = false;
return; return;
} }
var content = GenerateForType(e.NewValue.GetType()); var content = GenerateForType(e.NewValue.GetType());
content.DataContext = e.NewValue; content.DataContext = e.NewValue;
ScrollViewer.Content = content; ScrollViewer.Content = content;
TbFilter.IsEnabled = true;
} }
public Grid GenerateForType(Type t) public Grid GenerateForType(Type t)
@@ -73,9 +75,13 @@ namespace Torch.Views
foreach (var property in properties) foreach (var property in properties)
{ {
//Attempt to load our custom DisplayAttribute
var a = property.GetCustomAttribute<DisplayAttribute>(); var a = property.GetCustomAttribute<DisplayAttribute>();
if (IgnoreDisplay) //If not found and IgnoreDisplay is not set, fall back to system DisplayAttribute
a = null; if (a == null && !IgnoreDisplay)
a = property.GetCustomAttribute<System.ComponentModel.DataAnnotations.DisplayAttribute>();
if (a?.Visible == false)
continue;
descriptors[property] = a; descriptors[property] = a;
string category = a?.GroupName ?? "Misc"; string category = a?.GroupName ?? "Misc";
@@ -104,7 +110,13 @@ namespace Torch.Views
grid.Children.Add(cl); grid.Children.Add(cl);
curRow++; curRow++;
c.Value.Sort((a,b)=> string.Compare((descriptors[a]?.Name ?? a.Name), descriptors[b]?.Name ?? b.Name, StringComparison.Ordinal)); c.Value.Sort((a, b) =>
{
var c1 = descriptors[a]?.Order.CompareTo(descriptors[b]?.Order);
if (c1.HasValue && c1.Value != 0)
return c1.Value;
return string.Compare((descriptors[a]?.Name ?? a.Name), descriptors[b]?.Name ?? b.Name, StringComparison.Ordinal);
});
foreach (var property in c.Value) foreach (var property in c.Value)
{ {
@@ -120,7 +132,7 @@ namespace Torch.Views
var text = new TextBlock var text = new TextBlock
{ {
Text = displayName ?? property.Name, Text = displayName ?? property.Name,
ToolTip = displayName, ToolTip = descriptor?.ToolTip ?? displayName,
VerticalAlignment = VerticalAlignment.Center VerticalAlignment = VerticalAlignment.Center
}; };
text.SetValue(Grid.ColumnProperty, 0); text.SetValue(Grid.ColumnProperty, 0);
@@ -128,10 +140,12 @@ namespace Torch.Views
text.Margin = new Thickness(3); text.Margin = new Thickness(3);
text.Tag = $"{text.Text}: {descriptor?.Description}"; text.Tag = $"{text.Text}: {descriptor?.Description}";
text.IsMouseDirectlyOverChanged += Text_IsMouseDirectlyOverChanged; text.IsMouseDirectlyOverChanged += Text_IsMouseDirectlyOverChanged;
//if (descriptor?.Enabled == false)
// text.IsEnabled = false;
grid.Children.Add(text); grid.Children.Add(text);
FrameworkElement valueControl; FrameworkElement valueControl;
if (property.GetSetMethod() == null) if (property.GetSetMethod() == null || descriptor?.ReadOnly == true)
{ {
valueControl = new TextBlock(); valueControl = new TextBlock();
var binding = new Binding(property.Name) var binding = new Binding(property.Name)
@@ -218,6 +232,8 @@ namespace Torch.Views
valueControl.SetValue(Grid.ColumnProperty, 1); valueControl.SetValue(Grid.ColumnProperty, 1);
valueControl.SetValue(Grid.RowProperty, curRow); valueControl.SetValue(Grid.RowProperty, curRow);
valueControl.IsMouseDirectlyOverChanged += Text_IsMouseDirectlyOverChanged; valueControl.IsMouseDirectlyOverChanged += Text_IsMouseDirectlyOverChanged;
if (descriptor?.Enabled == false)
valueControl.IsEnabled = false;
grid.Children.Add(valueControl); grid.Children.Add(valueControl);
curRow++; curRow++;
@@ -260,6 +276,8 @@ namespace Torch.Views
{ {
var filterText = ((TextBox)sender).Text; var filterText = ((TextBox)sender).Text;
var grid = (Grid)ScrollViewer.Content; var grid = (Grid)ScrollViewer.Content;
if (grid == null)
return;
foreach (var child in grid.Children) foreach (var child in grid.Children)
{ {
if (!(child is TextBlock block)) if (!(child is TextBlock block))