From f2537706e768715268584776fb18aa39e87016c9 Mon Sep 17 00:00:00 2001 From: Brant Martin Date: Thu, 26 Apr 2018 08:20:37 -0400 Subject: [PATCH 1/3] Allow properties in PropertyGrid to set Enabled, Visible, ReadOnly, override sort order. --- Torch/Torch.csproj | 1 + Torch/Views/DisplayAttribute.cs | 34 ++++++++++++++++++++++++++++++++ Torch/Views/PropertyGrid.xaml.cs | 24 +++++++++++++++++----- 3 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 Torch/Views/DisplayAttribute.cs diff --git a/Torch/Torch.csproj b/Torch/Torch.csproj index 54e62a0..e9791ac 100644 --- a/Torch/Torch.csproj +++ b/Torch/Torch.csproj @@ -271,6 +271,7 @@ DictionaryEditor.xaml + ObjectCollectionEditor.xaml diff --git a/Torch/Views/DisplayAttribute.cs b/Torch/Views/DisplayAttribute.cs new file mode 100644 index 0000000..db75289 --- /dev/null +++ b/Torch/Views/DisplayAttribute.cs @@ -0,0 +1,34 @@ +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; + public bool? Visible; + public bool? ReadOnly; + + public DisplayAttribute() + { } + + public static implicit operator DisplayAttribute(System.ComponentModel.DataAnnotations.DisplayAttribute da) + { + return new DisplayAttribute() + { + Name = da.GetName(), + Description = da.GetDescription(), + GroupName = da.GetGroupName(), + Order = da.GetOrder() + }; + } + } +} diff --git a/Torch/Views/PropertyGrid.xaml.cs b/Torch/Views/PropertyGrid.xaml.cs index 23e2ef6..3168bd7 100644 --- a/Torch/Views/PropertyGrid.xaml.cs +++ b/Torch/Views/PropertyGrid.xaml.cs @@ -73,9 +73,13 @@ namespace Torch.Views foreach (var property in properties) { + //Attempt to load our custom DisplayAttribute var a = property.GetCustomAttribute(); - if (IgnoreDisplay) - a = null; + //If not found and IgnoreDisplay is not set, fall back to system DisplayAttribute + if (a == null && !IgnoreDisplay) + a = property.GetCustomAttribute(); + if (a?.Visible == false) + continue; descriptors[property] = a; string category = a?.GroupName ?? "Misc"; @@ -104,7 +108,13 @@ namespace Torch.Views grid.Children.Add(cl); 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) { @@ -120,7 +130,7 @@ namespace Torch.Views var text = new TextBlock { Text = displayName ?? property.Name, - ToolTip = displayName, + ToolTip = descriptor?.ToolTip ?? displayName, VerticalAlignment = VerticalAlignment.Center }; text.SetValue(Grid.ColumnProperty, 0); @@ -128,10 +138,12 @@ namespace Torch.Views text.Margin = new Thickness(3); text.Tag = $"{text.Text}: {descriptor?.Description}"; text.IsMouseDirectlyOverChanged += Text_IsMouseDirectlyOverChanged; + if (descriptor?.Enabled == false) + text.IsEnabled = false; grid.Children.Add(text); FrameworkElement valueControl; - if (property.GetSetMethod() == null) + if (property.GetSetMethod() == null || descriptor?.ReadOnly == true) { valueControl = new TextBlock(); var binding = new Binding(property.Name) @@ -218,6 +230,8 @@ namespace Torch.Views valueControl.SetValue(Grid.ColumnProperty, 1); valueControl.SetValue(Grid.RowProperty, curRow); valueControl.IsMouseDirectlyOverChanged += Text_IsMouseDirectlyOverChanged; + if (descriptor?.Enabled == false) + valueControl.IsEnabled = false; grid.Children.Add(valueControl); curRow++; From b9e9be227ab0937a3c0776fdbbac26003a66a831 Mon Sep 17 00:00:00 2001 From: Brant Martin Date: Fri, 27 Apr 2018 10:25:57 -0400 Subject: [PATCH 2/3] Tweak DisplayAttribute, don't disable text labels, PropertyGrid should be feature complete now. --- Torch/Views/DisplayAttribute.cs | 13 ++++++++----- Torch/Views/PropertyGrid.xaml.cs | 6 +++--- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/Torch/Views/DisplayAttribute.cs b/Torch/Views/DisplayAttribute.cs index db75289..9dda943 100644 --- a/Torch/Views/DisplayAttribute.cs +++ b/Torch/Views/DisplayAttribute.cs @@ -12,22 +12,25 @@ namespace Torch.Views public string Description; public string ToolTip; public string GroupName; - public int? Order; - public bool? Enabled; - public bool? Visible; - public bool? ReadOnly; + 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() + Order = da.GetOrder() ?? 0 }; } } diff --git a/Torch/Views/PropertyGrid.xaml.cs b/Torch/Views/PropertyGrid.xaml.cs index 3168bd7..4d44340 100644 --- a/Torch/Views/PropertyGrid.xaml.cs +++ b/Torch/Views/PropertyGrid.xaml.cs @@ -110,7 +110,7 @@ namespace Torch.Views c.Value.Sort((a, b) => { - var c1 = descriptors[a]?.Order?.CompareTo(descriptors[b]?.Order); + 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); @@ -138,8 +138,8 @@ namespace Torch.Views text.Margin = new Thickness(3); text.Tag = $"{text.Text}: {descriptor?.Description}"; text.IsMouseDirectlyOverChanged += Text_IsMouseDirectlyOverChanged; - if (descriptor?.Enabled == false) - text.IsEnabled = false; + //if (descriptor?.Enabled == false) + // text.IsEnabled = false; grid.Children.Add(text); FrameworkElement valueControl; From 7404b6bd2d2255fc49b200bcc93fd8304e499cd9 Mon Sep 17 00:00:00 2001 From: Brant Martin Date: Fri, 27 Apr 2018 10:26:17 -0400 Subject: [PATCH 3/3] Fix crash when trying to filter an empty PropertyGrid --- Torch/Views/PropertyGrid.xaml | 2 +- Torch/Views/PropertyGrid.xaml.cs | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/Torch/Views/PropertyGrid.xaml b/Torch/Views/PropertyGrid.xaml index 273c84f..78b24d5 100644 --- a/Torch/Views/PropertyGrid.xaml +++ b/Torch/Views/PropertyGrid.xaml @@ -17,7 +17,7 @@ - + diff --git a/Torch/Views/PropertyGrid.xaml.cs b/Torch/Views/PropertyGrid.xaml.cs index 4d44340..9e0bd39 100644 --- a/Torch/Views/PropertyGrid.xaml.cs +++ b/Torch/Views/PropertyGrid.xaml.cs @@ -49,12 +49,14 @@ namespace Torch.Views if (e.NewValue == null) { ScrollViewer.Content = null; + TbFilter.IsEnabled = false; return; } var content = GenerateForType(e.NewValue.GetType()); content.DataContext = e.NewValue; ScrollViewer.Content = content; + TbFilter.IsEnabled = true; } public Grid GenerateForType(Type t) @@ -274,6 +276,8 @@ namespace Torch.Views { var filterText = ((TextBox)sender).Text; var grid = (Grid)ScrollViewer.Content; + if (grid == null) + return; foreach (var child in grid.Children) { if (!(child is TextBlock block))