chore: use file-scoped namespace

This commit is contained in:
NaBian committed 2022-03-13 20:28:58 +08:00
1 parent 99d2fafb72
commit bbf1b7d4c6
748 files changed
+57929 -58678

No files matched your search

@@ -1,14 +1,13 @@
using System.Windows;
namespace HandyControl.Controls
{
public class DatePropertyEditor : PropertyEditorBase
{
public override FrameworkElement CreateElement(PropertyItem propertyItem) => new DateTimePicker
{
IsEnabled = !propertyItem.IsReadOnly
};
namespace HandyControl.Controls;
public override DependencyProperty GetDependencyProperty() => System.Windows.Controls.DatePicker.SelectedDateProperty;
}
}
public class DatePropertyEditor : PropertyEditorBase
{
public override FrameworkElement CreateElement(PropertyItem propertyItem) => new DateTimePicker
{
IsEnabled = !propertyItem.IsReadOnly
};
public override DependencyProperty GetDependencyProperty() => System.Windows.Controls.DatePicker.SelectedDateProperty;
}
@@ -1,14 +1,13 @@
using System.Windows;
namespace HandyControl.Controls
{
public class DateTimePropertyEditor : PropertyEditorBase
{
public override FrameworkElement CreateElement(PropertyItem propertyItem) => new DateTimePicker
{
IsEnabled = !propertyItem.IsReadOnly
};
namespace HandyControl.Controls;
public override DependencyProperty GetDependencyProperty() => DateTimePicker.SelectedDateTimeProperty;
}
}
public class DateTimePropertyEditor : PropertyEditorBase
{
public override FrameworkElement CreateElement(PropertyItem propertyItem) => new DateTimePicker
{
IsEnabled = !propertyItem.IsReadOnly
};
public override DependencyProperty GetDependencyProperty() => DateTimePicker.SelectedDateTimeProperty;
}
@@ -2,16 +2,15 @@
using System.Windows;
using System.Windows.Controls.Primitives;
namespace HandyControl.Controls
{
public class EnumPropertyEditor : PropertyEditorBase
{
public override FrameworkElement CreateElement(PropertyItem propertyItem) => new System.Windows.Controls.ComboBox
{
IsEnabled = !propertyItem.IsReadOnly,
ItemsSource = Enum.GetValues(propertyItem.PropertyType)
};
namespace HandyControl.Controls;
public override DependencyProperty GetDependencyProperty() => Selector.SelectedValueProperty;
}
}
public class EnumPropertyEditor : PropertyEditorBase
{
public override FrameworkElement CreateElement(PropertyItem propertyItem) => new System.Windows.Controls.ComboBox
{
IsEnabled = !propertyItem.IsReadOnly,
ItemsSource = Enum.GetValues(propertyItem.PropertyType)
};
public override DependencyProperty GetDependencyProperty() => Selector.SelectedValueProperty;
}
@@ -8,64 +8,63 @@ using System.Windows.Shapes;
using HandyControl.Data;
using HandyControl.Tools;
namespace HandyControl.Controls
namespace HandyControl.Controls;
public class HorizontalAlignmentPropertyEditor : PropertyEditorBase
{
public class HorizontalAlignmentPropertyEditor : PropertyEditorBase
public override FrameworkElement CreateElement(PropertyItem propertyItem) => new System.Windows.Controls.ComboBox
{
public override FrameworkElement CreateElement(PropertyItem propertyItem) => new System.Windows.Controls.ComboBox
{
Style = ResourceHelper.GetResourceInternal<Style>("ComboBoxCapsule"),
ItemsSource = Enum.GetValues(propertyItem.PropertyType),
ItemTemplateSelector = ResourceHelper.GetResourceInternal<DataTemplateSelector>("HorizontalAlignmentPathTemplateSelector"),
HorizontalAlignment = HorizontalAlignment.Left
};
Style = ResourceHelper.GetResourceInternal<Style>("ComboBoxCapsule"),
ItemsSource = Enum.GetValues(propertyItem.PropertyType),
ItemTemplateSelector = ResourceHelper.GetResourceInternal<DataTemplateSelector>("HorizontalAlignmentPathTemplateSelector"),
HorizontalAlignment = HorizontalAlignment.Left
};
public override DependencyProperty GetDependencyProperty() => Selector.SelectedValueProperty;
}
public override DependencyProperty GetDependencyProperty() => Selector.SelectedValueProperty;
}
public class HorizontalAlignmentPathTemplateSelector : DataTemplateSelector
public class HorizontalAlignmentPathTemplateSelector : DataTemplateSelector
{
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
public override DataTemplate SelectTemplate(object item, DependencyObject container)
if (item is HorizontalAlignment horizontalAlignment)
{
if (item is HorizontalAlignment horizontalAlignment)
var dataTemplate = new DataTemplate
{
var dataTemplate = new DataTemplate
{
DataType = typeof(System.Windows.Controls.ComboBox)
};
DataType = typeof(System.Windows.Controls.ComboBox)
};
var factory = new FrameworkElementFactory(typeof(Path));
factory.SetValue(FrameworkElement.WidthProperty, 12.0);
factory.SetValue(FrameworkElement.HeightProperty, ValueBoxes.Double10Box);
factory.SetBinding(Shape.FillProperty, new Binding(Control.ForegroundProperty.Name)
var factory = new FrameworkElementFactory(typeof(Path));
factory.SetValue(FrameworkElement.WidthProperty, 12.0);
factory.SetValue(FrameworkElement.HeightProperty, ValueBoxes.Double10Box);
factory.SetBinding(Shape.FillProperty, new Binding(Control.ForegroundProperty.Name)
{
RelativeSource = new RelativeSource
{
RelativeSource = new RelativeSource
{
AncestorType = typeof(ComboBoxItem)
}
});
switch (horizontalAlignment)
{
case HorizontalAlignment.Left:
factory.SetValue(Path.DataProperty, ResourceHelper.GetResourceInternal<Geometry>("AlignLeftGeometry"));
break;
case HorizontalAlignment.Center:
factory.SetValue(Path.DataProperty, ResourceHelper.GetResourceInternal<Geometry>("AlignHCenterGeometry"));
break;
case HorizontalAlignment.Right:
factory.SetValue(Path.DataProperty, ResourceHelper.GetResourceInternal<Geometry>("AlignRightGeometry"));
break;
case HorizontalAlignment.Stretch:
factory.SetValue(Path.DataProperty, ResourceHelper.GetResourceInternal<Geometry>("AlignHStretchGeometry"));
break;
AncestorType = typeof(ComboBoxItem)
}
});
dataTemplate.VisualTree = factory;
return dataTemplate;
switch (horizontalAlignment)
{
case HorizontalAlignment.Left:
factory.SetValue(Path.DataProperty, ResourceHelper.GetResourceInternal<Geometry>("AlignLeftGeometry"));
break;
case HorizontalAlignment.Center:
factory.SetValue(Path.DataProperty, ResourceHelper.GetResourceInternal<Geometry>("AlignHCenterGeometry"));
break;
case HorizontalAlignment.Right:
factory.SetValue(Path.DataProperty, ResourceHelper.GetResourceInternal<Geometry>("AlignRightGeometry"));
break;
case HorizontalAlignment.Stretch:
factory.SetValue(Path.DataProperty, ResourceHelper.GetResourceInternal<Geometry>("AlignHStretchGeometry"));
break;
}
return null;
dataTemplate.VisualTree = factory;
return dataTemplate;
}
return null;
}
}
}
@@ -4,60 +4,59 @@ using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace HandyControl.Controls
{
public class ImagePropertyEditor : PropertyEditorBase
{
public override FrameworkElement CreateElement(PropertyItem propertyItem)
{
var imageSelector = new ImageSelector
{
IsEnabled = !propertyItem.IsReadOnly,
Width = 50,
Height = 50,
HorizontalAlignment = HorizontalAlignment.Left
};
namespace HandyControl.Controls;
BindingOperations.SetBinding(this, UriProperty, new Binding(ImageSelector.UriProperty.Name)
public class ImagePropertyEditor : PropertyEditorBase
{
public override FrameworkElement CreateElement(PropertyItem propertyItem)
{
var imageSelector = new ImageSelector
{
IsEnabled = !propertyItem.IsReadOnly,
Width = 50,
Height = 50,
HorizontalAlignment = HorizontalAlignment.Left
};
BindingOperations.SetBinding(this, UriProperty, new Binding(ImageSelector.UriProperty.Name)
{
Source = imageSelector,
Mode = BindingMode.OneWay
});
return imageSelector;
}
internal static readonly DependencyProperty UriProperty = DependencyProperty.Register(
"Uri", typeof(Uri), typeof(ImagePropertyEditor), new PropertyMetadata(default(Uri), OnUriChangedCallback));
private static void OnUriChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) =>
((ImagePropertyEditor) d).Source = e.NewValue is Uri uri ? BitmapFrame.Create(uri) : null;
internal Uri Uri
{
get => (Uri) GetValue(UriProperty);
set => SetValue(UriProperty, value);
}
public static readonly DependencyProperty SourceProperty = DependencyProperty.Register(
"Source", typeof(ImageSource), typeof(ImagePropertyEditor), new PropertyMetadata(default(ImageSource)));
public ImageSource Source
{
get => (ImageSource) GetValue(SourceProperty);
set => SetValue(SourceProperty, value);
}
public override void CreateBinding(PropertyItem propertyItem, DependencyObject element)
=> BindingOperations.SetBinding(this, GetDependencyProperty(),
new Binding($"({propertyItem.PropertyName})")
{
Source = imageSelector,
Mode = BindingMode.OneWay
Source = propertyItem.Value,
Mode = GetBindingMode(propertyItem),
UpdateSourceTrigger = GetUpdateSourceTrigger(propertyItem),
Converter = GetConverter(propertyItem)
});
return imageSelector;
}
internal static readonly DependencyProperty UriProperty = DependencyProperty.Register(
"Uri", typeof(Uri), typeof(ImagePropertyEditor), new PropertyMetadata(default(Uri), OnUriChangedCallback));
private static void OnUriChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e) =>
((ImagePropertyEditor) d).Source = e.NewValue is Uri uri ? BitmapFrame.Create(uri) : null;
internal Uri Uri
{
get => (Uri) GetValue(UriProperty);
set => SetValue(UriProperty, value);
}
public static readonly DependencyProperty SourceProperty = DependencyProperty.Register(
"Source", typeof(ImageSource), typeof(ImagePropertyEditor), new PropertyMetadata(default(ImageSource)));
public ImageSource Source
{
get => (ImageSource) GetValue(SourceProperty);
set => SetValue(SourceProperty, value);
}
public override void CreateBinding(PropertyItem propertyItem, DependencyObject element)
=> BindingOperations.SetBinding(this, GetDependencyProperty(),
new Binding($"({propertyItem.PropertyName})")
{
Source = propertyItem.Value,
Mode = GetBindingMode(propertyItem),
UpdateSourceTrigger = GetUpdateSourceTrigger(propertyItem),
Converter = GetConverter(propertyItem)
});
public override DependencyProperty GetDependencyProperty() => SourceProperty;
}
}
public override DependencyProperty GetDependencyProperty() => SourceProperty;
}
@@ -1,31 +1,30 @@
using System.Windows;
namespace HandyControl.Controls
namespace HandyControl.Controls;
public class NumberPropertyEditor : PropertyEditorBase
{
public class NumberPropertyEditor : PropertyEditorBase
public NumberPropertyEditor()
{
public NumberPropertyEditor()
{
}
public NumberPropertyEditor(double minimum, double maximum)
{
Minimum = minimum;
Maximum = maximum;
}
public double Minimum { get; set; }
public double Maximum { get; set; }
public override FrameworkElement CreateElement(PropertyItem propertyItem) => new NumericUpDown
{
IsReadOnly = propertyItem.IsReadOnly,
Minimum = Minimum,
Maximum = Maximum
};
public override DependencyProperty GetDependencyProperty() => NumericUpDown.ValueProperty;
}
}
public NumberPropertyEditor(double minimum, double maximum)
{
Minimum = minimum;
Maximum = maximum;
}
public double Minimum { get; set; }
public double Maximum { get; set; }
public override FrameworkElement CreateElement(PropertyItem propertyItem) => new NumericUpDown
{
IsReadOnly = propertyItem.IsReadOnly,
Minimum = Minimum,
Maximum = Maximum
};
public override DependencyProperty GetDependencyProperty() => NumericUpDown.ValueProperty;
}
@@ -1,14 +1,13 @@
using System.Windows;
namespace HandyControl.Controls
{
public class PlainTextPropertyEditor : PropertyEditorBase
{
public override FrameworkElement CreateElement(PropertyItem propertyItem) => new System.Windows.Controls.TextBox
{
IsReadOnly = propertyItem.IsReadOnly
};
namespace HandyControl.Controls;
public override DependencyProperty GetDependencyProperty() => System.Windows.Controls.TextBox.TextProperty;
}
}
public class PlainTextPropertyEditor : PropertyEditorBase
{
public override FrameworkElement CreateElement(PropertyItem propertyItem) => new System.Windows.Controls.TextBox
{
IsReadOnly = propertyItem.IsReadOnly
};
public override DependencyProperty GetDependencyProperty() => System.Windows.Controls.TextBox.TextProperty;
}
@@ -1,28 +1,27 @@
using System.Windows;
using System.Windows.Data;
namespace HandyControl.Controls
namespace HandyControl.Controls;
public abstract class PropertyEditorBase : DependencyObject
{
public abstract class PropertyEditorBase : DependencyObject
{
public abstract FrameworkElement CreateElement(PropertyItem propertyItem);
public abstract FrameworkElement CreateElement(PropertyItem propertyItem);
public virtual void CreateBinding(PropertyItem propertyItem, DependencyObject element) =>
BindingOperations.SetBinding(element, GetDependencyProperty(),
new Binding($"{propertyItem.PropertyName}")
{
Source = propertyItem.Value,
Mode = GetBindingMode(propertyItem),
UpdateSourceTrigger = GetUpdateSourceTrigger(propertyItem),
Converter = GetConverter(propertyItem)
});
public virtual void CreateBinding(PropertyItem propertyItem, DependencyObject element) =>
BindingOperations.SetBinding(element, GetDependencyProperty(),
new Binding($"{propertyItem.PropertyName}")
{
Source = propertyItem.Value,
Mode = GetBindingMode(propertyItem),
UpdateSourceTrigger = GetUpdateSourceTrigger(propertyItem),
Converter = GetConverter(propertyItem)
});
public abstract DependencyProperty GetDependencyProperty();
public abstract DependencyProperty GetDependencyProperty();
public virtual BindingMode GetBindingMode(PropertyItem propertyItem) => propertyItem.IsReadOnly ? BindingMode.OneWay : BindingMode.TwoWay;
public virtual BindingMode GetBindingMode(PropertyItem propertyItem) => propertyItem.IsReadOnly ? BindingMode.OneWay : BindingMode.TwoWay;
public virtual UpdateSourceTrigger GetUpdateSourceTrigger(PropertyItem propertyItem) => UpdateSourceTrigger.PropertyChanged;
public virtual UpdateSourceTrigger GetUpdateSourceTrigger(PropertyItem propertyItem) => UpdateSourceTrigger.PropertyChanged;
protected virtual IValueConverter GetConverter(PropertyItem propertyItem) => null;
}
}
protected virtual IValueConverter GetConverter(PropertyItem propertyItem) => null;
}
@@ -2,19 +2,18 @@
using System.Windows.Data;
using HandyControl.Tools;
namespace HandyControl.Controls
namespace HandyControl.Controls;
public class ReadOnlyTextPropertyEditor : PropertyEditorBase
{
public class ReadOnlyTextPropertyEditor : PropertyEditorBase
public override FrameworkElement CreateElement(PropertyItem propertyItem) => new System.Windows.Controls.TextBox
{
public override FrameworkElement CreateElement(PropertyItem propertyItem) => new System.Windows.Controls.TextBox
{
IsReadOnly = true
};
IsReadOnly = true
};
public override DependencyProperty GetDependencyProperty() => System.Windows.Controls.TextBox.TextProperty;
public override DependencyProperty GetDependencyProperty() => System.Windows.Controls.TextBox.TextProperty;
public override BindingMode GetBindingMode(PropertyItem propertyItem) => BindingMode.OneWay;
public override BindingMode GetBindingMode(PropertyItem propertyItem) => BindingMode.OneWay;
protected override IValueConverter GetConverter(PropertyItem propertyItem) => ResourceHelper.GetResourceInternal<IValueConverter>("Object2StringConverter");
}
}
protected override IValueConverter GetConverter(PropertyItem propertyItem) => ResourceHelper.GetResourceInternal<IValueConverter>("Object2StringConverter");
}
@@ -2,17 +2,16 @@
using System.Windows.Controls.Primitives;
using HandyControl.Tools;
namespace HandyControl.Controls
{
public class SwitchPropertyEditor : PropertyEditorBase
{
public override FrameworkElement CreateElement(PropertyItem propertyItem) => new ToggleButton
{
Style = ResourceHelper.GetResourceInternal<Style>("ToggleButtonSwitch"),
HorizontalAlignment = HorizontalAlignment.Left,
IsEnabled = !propertyItem.IsReadOnly
};
namespace HandyControl.Controls;
public override DependencyProperty GetDependencyProperty() => ToggleButton.IsCheckedProperty;
}
}
public class SwitchPropertyEditor : PropertyEditorBase
{
public override FrameworkElement CreateElement(PropertyItem propertyItem) => new ToggleButton
{
Style = ResourceHelper.GetResourceInternal<Style>("ToggleButtonSwitch"),
HorizontalAlignment = HorizontalAlignment.Left,
IsEnabled = !propertyItem.IsReadOnly
};
public override DependencyProperty GetDependencyProperty() => ToggleButton.IsCheckedProperty;
}
@@ -1,14 +1,13 @@
using System.Windows;
namespace HandyControl.Controls
{
public class TimePropertyEditor : PropertyEditorBase
{
public override FrameworkElement CreateElement(PropertyItem propertyItem) => new DateTimePicker
{
IsEnabled = !propertyItem.IsReadOnly
};
namespace HandyControl.Controls;
public override DependencyProperty GetDependencyProperty() => TimePicker.SelectedTimeProperty;
}
}
public class TimePropertyEditor : PropertyEditorBase
{
public override FrameworkElement CreateElement(PropertyItem propertyItem) => new DateTimePicker
{
IsEnabled = !propertyItem.IsReadOnly
};
public override DependencyProperty GetDependencyProperty() => TimePicker.SelectedTimeProperty;
}
@@ -8,64 +8,63 @@ using System.Windows.Shapes;
using HandyControl.Data;
using HandyControl.Tools;
namespace HandyControl.Controls
namespace HandyControl.Controls;
public class VerticalAlignmentPropertyEditor : PropertyEditorBase
{
public class VerticalAlignmentPropertyEditor : PropertyEditorBase
public override FrameworkElement CreateElement(PropertyItem propertyItem) => new System.Windows.Controls.ComboBox
{
public override FrameworkElement CreateElement(PropertyItem propertyItem) => new System.Windows.Controls.ComboBox
{
Style = ResourceHelper.GetResourceInternal<Style>("ComboBoxCapsule"),
ItemsSource = Enum.GetValues(propertyItem.PropertyType),
ItemTemplateSelector = ResourceHelper.GetResourceInternal<DataTemplateSelector>("VerticalAlignmentPathTemplateSelector"),
HorizontalAlignment = HorizontalAlignment.Left
};
Style = ResourceHelper.GetResourceInternal<Style>("ComboBoxCapsule"),
ItemsSource = Enum.GetValues(propertyItem.PropertyType),
ItemTemplateSelector = ResourceHelper.GetResourceInternal<DataTemplateSelector>("VerticalAlignmentPathTemplateSelector"),
HorizontalAlignment = HorizontalAlignment.Left
};
public override DependencyProperty GetDependencyProperty() => Selector.SelectedValueProperty;
}
public override DependencyProperty GetDependencyProperty() => Selector.SelectedValueProperty;
}
public class VerticalAlignmentPathTemplateSelector : DataTemplateSelector
public class VerticalAlignmentPathTemplateSelector : DataTemplateSelector
{
public override DataTemplate SelectTemplate(object item, DependencyObject container)
{
public override DataTemplate SelectTemplate(object item, DependencyObject container)
if (item is VerticalAlignment verticalAlignment)
{
if (item is VerticalAlignment verticalAlignment)
var dataTemplate = new DataTemplate
{
var dataTemplate = new DataTemplate
{
DataType = typeof(System.Windows.Controls.ComboBox)
};
DataType = typeof(System.Windows.Controls.ComboBox)
};
var factory = new FrameworkElementFactory(typeof(Path));
factory.SetValue(FrameworkElement.WidthProperty, ValueBoxes.Double10Box);
factory.SetValue(FrameworkElement.HeightProperty, 12.0);
factory.SetBinding(Shape.FillProperty, new Binding(Control.ForegroundProperty.Name)
var factory = new FrameworkElementFactory(typeof(Path));
factory.SetValue(FrameworkElement.WidthProperty, ValueBoxes.Double10Box);
factory.SetValue(FrameworkElement.HeightProperty, 12.0);
factory.SetBinding(Shape.FillProperty, new Binding(Control.ForegroundProperty.Name)
{
RelativeSource = new RelativeSource
{
RelativeSource = new RelativeSource
{
AncestorType = typeof(ComboBoxItem)
}
});
switch (verticalAlignment)
{
case VerticalAlignment.Top:
factory.SetValue(Path.DataProperty, ResourceHelper.GetResourceInternal<Geometry>("AlignTopGeometry"));
break;
case VerticalAlignment.Center:
factory.SetValue(Path.DataProperty, ResourceHelper.GetResourceInternal<Geometry>("AlignVCenterGeometry"));
break;
case VerticalAlignment.Bottom:
factory.SetValue(Path.DataProperty, ResourceHelper.GetResourceInternal<Geometry>("AlignBottomGeometry"));
break;
case VerticalAlignment.Stretch:
factory.SetValue(Path.DataProperty, ResourceHelper.GetResourceInternal<Geometry>("AlignVStretchGeometry"));
break;
AncestorType = typeof(ComboBoxItem)
}
});
dataTemplate.VisualTree = factory;
return dataTemplate;
switch (verticalAlignment)
{
case VerticalAlignment.Top:
factory.SetValue(Path.DataProperty, ResourceHelper.GetResourceInternal<Geometry>("AlignTopGeometry"));
break;
case VerticalAlignment.Center:
factory.SetValue(Path.DataProperty, ResourceHelper.GetResourceInternal<Geometry>("AlignVCenterGeometry"));
break;
case VerticalAlignment.Bottom:
factory.SetValue(Path.DataProperty, ResourceHelper.GetResourceInternal<Geometry>("AlignBottomGeometry"));
break;
case VerticalAlignment.Stretch:
factory.SetValue(Path.DataProperty, ResourceHelper.GetResourceInternal<Geometry>("AlignVStretchGeometry"));
break;
}
return null;
dataTemplate.VisualTree = factory;
return dataTemplate;
}
return null;
}
}
}
@@ -9,196 +9,195 @@ using HandyControl.Data;
using HandyControl.Interactivity;
using HandyControl.Tools.Extension;
namespace HandyControl.Controls
namespace HandyControl.Controls;
[TemplatePart(Name = ElementItemsControl, Type = typeof(ItemsControl))]
[TemplatePart(Name = ElementSearchBar, Type = typeof(SearchBar))]
public class PropertyGrid : Control
{
[TemplatePart(Name = ElementItemsControl, Type = typeof(ItemsControl))]
[TemplatePart(Name = ElementSearchBar, Type = typeof(SearchBar))]
public class PropertyGrid : Control
private const string ElementItemsControl = "PART_ItemsControl";
private const string ElementSearchBar = "PART_SearchBar";
private ItemsControl _itemsControl;
private ICollectionView _dataView;
private SearchBar _searchBar;
private string _searchKey;
public PropertyGrid()
{
private const string ElementItemsControl = "PART_ItemsControl";
CommandBindings.Add(new CommandBinding(ControlCommands.SortByCategory, SortByCategory, (s, e) => e.CanExecute = ShowSortButton));
CommandBindings.Add(new CommandBinding(ControlCommands.SortByName, SortByName, (s, e) => e.CanExecute = ShowSortButton));
}
private const string ElementSearchBar = "PART_SearchBar";
public virtual PropertyResolver PropertyResolver { get; } = new();
private ItemsControl _itemsControl;
public static readonly RoutedEvent SelectedObjectChangedEvent =
EventManager.RegisterRoutedEvent("SelectedObjectChanged", RoutingStrategy.Bubble,
typeof(RoutedPropertyChangedEventHandler<object>), typeof(PropertyGrid));
private ICollectionView _dataView;
public event RoutedPropertyChangedEventHandler<object> SelectedObjectChanged
{
add => AddHandler(SelectedObjectChangedEvent, value);
remove => RemoveHandler(SelectedObjectChangedEvent, value);
}
private SearchBar _searchBar;
public static readonly DependencyProperty SelectedObjectProperty = DependencyProperty.Register(
"SelectedObject", typeof(object), typeof(PropertyGrid), new PropertyMetadata(default, OnSelectedObjectChanged));
private string _searchKey;
private static void OnSelectedObjectChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var ctl = (PropertyGrid) d;
ctl.OnSelectedObjectChanged(e.OldValue, e.NewValue);
}
public PropertyGrid()
public object SelectedObject
{
get => GetValue(SelectedObjectProperty);
set => SetValue(SelectedObjectProperty, value);
}
protected virtual void OnSelectedObjectChanged(object oldValue, object newValue)
{
UpdateItems(newValue);
RaiseEvent(new RoutedPropertyChangedEventArgs<object>(oldValue, newValue, SelectedObjectChangedEvent));
}
public static readonly DependencyProperty DescriptionProperty = DependencyProperty.Register(
"Description", typeof(string), typeof(PropertyGrid), new PropertyMetadata(default(string)));
public string Description
{
get => (string) GetValue(DescriptionProperty);
set => SetValue(DescriptionProperty, value);
}
public static readonly DependencyProperty MaxTitleWidthProperty = DependencyProperty.Register(
"MaxTitleWidth", typeof(double), typeof(PropertyGrid), new PropertyMetadata(ValueBoxes.Double0Box));
public double MaxTitleWidth
{
get => (double) GetValue(MaxTitleWidthProperty);
set => SetValue(MaxTitleWidthProperty, value);
}
public static readonly DependencyProperty MinTitleWidthProperty = DependencyProperty.Register(
"MinTitleWidth", typeof(double), typeof(PropertyGrid), new PropertyMetadata(ValueBoxes.Double0Box));
public double MinTitleWidth
{
get => (double) GetValue(MinTitleWidthProperty);
set => SetValue(MinTitleWidthProperty, value);
}
public static readonly DependencyProperty ShowSortButtonProperty = DependencyProperty.Register(
"ShowSortButton", typeof(bool), typeof(PropertyGrid), new PropertyMetadata(ValueBoxes.TrueBox));
public bool ShowSortButton
{
get => (bool) GetValue(ShowSortButtonProperty);
set => SetValue(ShowSortButtonProperty, ValueBoxes.BooleanBox(value));
}
public override void OnApplyTemplate()
{
if (_searchBar != null)
{
CommandBindings.Add(new CommandBinding(ControlCommands.SortByCategory, SortByCategory, (s, e) => e.CanExecute = ShowSortButton));
CommandBindings.Add(new CommandBinding(ControlCommands.SortByName, SortByName, (s, e) => e.CanExecute = ShowSortButton));
_searchBar.SearchStarted -= SearchBar_SearchStarted;
}
public virtual PropertyResolver PropertyResolver { get; } = new();
base.OnApplyTemplate();
public static readonly RoutedEvent SelectedObjectChangedEvent =
EventManager.RegisterRoutedEvent("SelectedObjectChanged", RoutingStrategy.Bubble,
typeof(RoutedPropertyChangedEventHandler<object>), typeof(PropertyGrid));
_itemsControl = GetTemplateChild(ElementItemsControl) as ItemsControl;
_searchBar = GetTemplateChild(ElementSearchBar) as SearchBar;
public event RoutedPropertyChangedEventHandler<object> SelectedObjectChanged
if (_searchBar != null)
{
add => AddHandler(SelectedObjectChangedEvent, value);
remove => RemoveHandler(SelectedObjectChangedEvent, value);
_searchBar.SearchStarted += SearchBar_SearchStarted;
}
public static readonly DependencyProperty SelectedObjectProperty = DependencyProperty.Register(
"SelectedObject", typeof(object), typeof(PropertyGrid), new PropertyMetadata(default, OnSelectedObjectChanged));
UpdateItems(SelectedObject);
}
private static void OnSelectedObjectChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
private void UpdateItems(object obj)
{
if (obj == null || _itemsControl == null) return;
_dataView = CollectionViewSource.GetDefaultView(TypeDescriptor.GetProperties(obj.GetType()).OfType<PropertyDescriptor>()
.Where(item => PropertyResolver.ResolveIsBrowsable(item)).Select(CreatePropertyItem)
.Do(item => item.InitElement()));
SortByCategory(null, null);
_itemsControl.ItemsSource = _dataView;
}
private void SortByCategory(object sender, ExecutedRoutedEventArgs e)
{
if (_dataView == null) return;
using (_dataView.DeferRefresh())
{
var ctl = (PropertyGrid) d;
ctl.OnSelectedObjectChanged(e.OldValue, e.NewValue);
}
public object SelectedObject
{
get => GetValue(SelectedObjectProperty);
set => SetValue(SelectedObjectProperty, value);
}
protected virtual void OnSelectedObjectChanged(object oldValue, object newValue)
{
UpdateItems(newValue);
RaiseEvent(new RoutedPropertyChangedEventArgs<object>(oldValue, newValue, SelectedObjectChangedEvent));
}
public static readonly DependencyProperty DescriptionProperty = DependencyProperty.Register(
"Description", typeof(string), typeof(PropertyGrid), new PropertyMetadata(default(string)));
public string Description
{
get => (string) GetValue(DescriptionProperty);
set => SetValue(DescriptionProperty, value);
}
public static readonly DependencyProperty MaxTitleWidthProperty = DependencyProperty.Register(
"MaxTitleWidth", typeof(double), typeof(PropertyGrid), new PropertyMetadata(ValueBoxes.Double0Box));
public double MaxTitleWidth
{
get => (double) GetValue(MaxTitleWidthProperty);
set => SetValue(MaxTitleWidthProperty, value);
}
public static readonly DependencyProperty MinTitleWidthProperty = DependencyProperty.Register(
"MinTitleWidth", typeof(double), typeof(PropertyGrid), new PropertyMetadata(ValueBoxes.Double0Box));
public double MinTitleWidth
{
get => (double) GetValue(MinTitleWidthProperty);
set => SetValue(MinTitleWidthProperty, value);
}
public static readonly DependencyProperty ShowSortButtonProperty = DependencyProperty.Register(
"ShowSortButton", typeof(bool), typeof(PropertyGrid), new PropertyMetadata(ValueBoxes.TrueBox));
public bool ShowSortButton
{
get => (bool) GetValue(ShowSortButtonProperty);
set => SetValue(ShowSortButtonProperty, ValueBoxes.BooleanBox(value));
}
public override void OnApplyTemplate()
{
if (_searchBar != null)
{
_searchBar.SearchStarted -= SearchBar_SearchStarted;
}
base.OnApplyTemplate();
_itemsControl = GetTemplateChild(ElementItemsControl) as ItemsControl;
_searchBar = GetTemplateChild(ElementSearchBar) as SearchBar;
if (_searchBar != null)
{
_searchBar.SearchStarted += SearchBar_SearchStarted;
}
UpdateItems(SelectedObject);
}
private void UpdateItems(object obj)
{
if (obj == null || _itemsControl == null) return;
_dataView = CollectionViewSource.GetDefaultView(TypeDescriptor.GetProperties(obj.GetType()).OfType<PropertyDescriptor>()
.Where(item => PropertyResolver.ResolveIsBrowsable(item)).Select(CreatePropertyItem)
.Do(item => item.InitElement()));
SortByCategory(null, null);
_itemsControl.ItemsSource = _dataView;
}
private void SortByCategory(object sender, ExecutedRoutedEventArgs e)
{
if (_dataView == null) return;
using (_dataView.DeferRefresh())
{
_dataView.GroupDescriptions.Clear();
_dataView.SortDescriptions.Clear();
_dataView.SortDescriptions.Add(new SortDescription(PropertyItem.CategoryProperty.Name, ListSortDirection.Ascending));
_dataView.SortDescriptions.Add(new SortDescription(PropertyItem.DisplayNameProperty.Name, ListSortDirection.Ascending));
_dataView.GroupDescriptions.Add(new PropertyGroupDescription(PropertyItem.CategoryProperty.Name));
}
}
private void SortByName(object sender, ExecutedRoutedEventArgs e)
{
if (_dataView == null) return;
using (_dataView.DeferRefresh())
{
_dataView.GroupDescriptions.Clear();
_dataView.SortDescriptions.Clear();
_dataView.SortDescriptions.Add(new SortDescription(PropertyItem.PropertyNameProperty.Name, ListSortDirection.Ascending));
}
}
private void SearchBar_SearchStarted(object sender, FunctionEventArgs<string> e)
{
if (_dataView == null) return;
_searchKey = e.Info;
if (string.IsNullOrEmpty(_searchKey))
{
foreach (UIElement item in _dataView)
{
item.Show();
}
}
else
{
foreach (PropertyItem item in _dataView)
{
item.Show(item.PropertyName.ToLower().Contains(_searchKey) || item.DisplayName.ToLower().Contains(_searchKey));
}
}
}
protected virtual PropertyItem CreatePropertyItem(PropertyDescriptor propertyDescriptor) => new()
{
Category = PropertyResolver.ResolveCategory(propertyDescriptor),
DisplayName = PropertyResolver.ResolveDisplayName(propertyDescriptor),
Description = PropertyResolver.ResolveDescription(propertyDescriptor),
IsReadOnly = PropertyResolver.ResolveIsReadOnly(propertyDescriptor),
DefaultValue = PropertyResolver.ResolveDefaultValue(propertyDescriptor),
Editor = PropertyResolver.ResolveEditor(propertyDescriptor),
Value = SelectedObject,
PropertyName = propertyDescriptor.Name,
PropertyType = propertyDescriptor.PropertyType,
PropertyTypeName = $"{propertyDescriptor.PropertyType.Namespace}.{propertyDescriptor.PropertyType.Name}"
};
protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
{
base.OnRenderSizeChanged(sizeInfo);
TitleElement.SetTitleWidth(this, new GridLength(Math.Max(MinTitleWidth, Math.Min(MaxTitleWidth, ActualWidth / 3))));
_dataView.GroupDescriptions.Clear();
_dataView.SortDescriptions.Clear();
_dataView.SortDescriptions.Add(new SortDescription(PropertyItem.CategoryProperty.Name, ListSortDirection.Ascending));
_dataView.SortDescriptions.Add(new SortDescription(PropertyItem.DisplayNameProperty.Name, ListSortDirection.Ascending));
_dataView.GroupDescriptions.Add(new PropertyGroupDescription(PropertyItem.CategoryProperty.Name));
}
}
}
private void SortByName(object sender, ExecutedRoutedEventArgs e)
{
if (_dataView == null) return;
using (_dataView.DeferRefresh())
{
_dataView.GroupDescriptions.Clear();
_dataView.SortDescriptions.Clear();
_dataView.SortDescriptions.Add(new SortDescription(PropertyItem.PropertyNameProperty.Name, ListSortDirection.Ascending));
}
}
private void SearchBar_SearchStarted(object sender, FunctionEventArgs<string> e)
{
if (_dataView == null) return;
_searchKey = e.Info;
if (string.IsNullOrEmpty(_searchKey))
{
foreach (UIElement item in _dataView)
{
item.Show();
}
}
else
{
foreach (PropertyItem item in _dataView)
{
item.Show(item.PropertyName.ToLower().Contains(_searchKey) || item.DisplayName.ToLower().Contains(_searchKey));
}
}
}
protected virtual PropertyItem CreatePropertyItem(PropertyDescriptor propertyDescriptor) => new()
{
Category = PropertyResolver.ResolveCategory(propertyDescriptor),
DisplayName = PropertyResolver.ResolveDisplayName(propertyDescriptor),
Description = PropertyResolver.ResolveDescription(propertyDescriptor),
IsReadOnly = PropertyResolver.ResolveIsReadOnly(propertyDescriptor),
DefaultValue = PropertyResolver.ResolveDefaultValue(propertyDescriptor),
Editor = PropertyResolver.ResolveEditor(propertyDescriptor),
Value = SelectedObject,
PropertyName = propertyDescriptor.Name,
PropertyType = propertyDescriptor.PropertyType,
PropertyTypeName = $"{propertyDescriptor.PropertyType.Namespace}.{propertyDescriptor.PropertyType.Name}"
};
protected override void OnRenderSizeChanged(SizeChangedInfo sizeInfo)
{
base.OnRenderSizeChanged(sizeInfo);
TitleElement.SetTitleWidth(this, new GridLength(Math.Max(MinTitleWidth, Math.Min(MaxTitleWidth, ActualWidth / 3))));
}
}
@@ -4,125 +4,124 @@ using System.Windows;
using System.Windows.Controls;
using HandyControl.Data;
namespace HandyControl.Controls
namespace HandyControl.Controls;
public class PropertyItem : ListBoxItem
{
public class PropertyItem : ListBoxItem
public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(
"Value", typeof(object), typeof(PropertyItem), new PropertyMetadata(default(object)));
public object Value
{
public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(
"Value", typeof(object), typeof(PropertyItem), new PropertyMetadata(default(object)));
public object Value
{
get => GetValue(ValueProperty);
set => SetValue(ValueProperty, value);
}
public static readonly DependencyProperty DisplayNameProperty = DependencyProperty.Register(
"DisplayName", typeof(string), typeof(PropertyItem), new PropertyMetadata(default(string)));
public string DisplayName
{
get => (string) GetValue(DisplayNameProperty);
set => SetValue(DisplayNameProperty, value);
}
public static readonly DependencyProperty PropertyNameProperty = DependencyProperty.Register(
"PropertyName", typeof(string), typeof(PropertyItem), new PropertyMetadata(default(string)));
public string PropertyName
{
get => (string) GetValue(PropertyNameProperty);
set => SetValue(PropertyNameProperty, value);
}
public static readonly DependencyProperty PropertyTypeProperty = DependencyProperty.Register(
"PropertyType", typeof(Type), typeof(PropertyItem), new PropertyMetadata(default(Type)));
public Type PropertyType
{
get => (Type) GetValue(PropertyTypeProperty);
set => SetValue(PropertyTypeProperty, value);
}
public static readonly DependencyProperty PropertyTypeNameProperty = DependencyProperty.Register(
"PropertyTypeName", typeof(string), typeof(PropertyItem), new PropertyMetadata(default(string)));
public string PropertyTypeName
{
get => (string) GetValue(PropertyTypeNameProperty);
set => SetValue(PropertyTypeNameProperty, value);
}
public static readonly DependencyProperty DescriptionProperty = DependencyProperty.Register(
"Description", typeof(string), typeof(PropertyItem), new PropertyMetadata(default(string)));
public string Description
{
get => (string) GetValue(DescriptionProperty);
set => SetValue(DescriptionProperty, value);
}
public static readonly DependencyProperty IsReadOnlyProperty = DependencyProperty.Register(
"IsReadOnly", typeof(bool), typeof(PropertyItem), new PropertyMetadata(ValueBoxes.FalseBox));
public bool IsReadOnly
{
get => (bool) GetValue(IsReadOnlyProperty);
set => SetValue(IsReadOnlyProperty, ValueBoxes.BooleanBox(value));
}
public static readonly DependencyProperty DefaultValueProperty = DependencyProperty.Register(
"DefaultValue", typeof(object), typeof(PropertyItem), new PropertyMetadata(default(object)));
public object DefaultValue
{
get => GetValue(DefaultValueProperty);
set => SetValue(DefaultValueProperty, value);
}
public static readonly DependencyProperty CategoryProperty = DependencyProperty.Register(
"Category", typeof(string), typeof(PropertyItem), new PropertyMetadata(default(string)));
public string Category
{
get => (string) GetValue(CategoryProperty);
set => SetValue(CategoryProperty, value);
}
public static readonly DependencyProperty EditorProperty = DependencyProperty.Register(
"Editor", typeof(PropertyEditorBase), typeof(PropertyItem), new PropertyMetadata(default(PropertyEditorBase)));
public PropertyEditorBase Editor
{
get => (PropertyEditorBase) GetValue(EditorProperty);
set => SetValue(EditorProperty, value);
}
public static readonly DependencyProperty EditorElementProperty = DependencyProperty.Register(
"EditorElement", typeof(FrameworkElement), typeof(PropertyItem), new PropertyMetadata(default(FrameworkElement)));
public FrameworkElement EditorElement
{
get => (FrameworkElement) GetValue(EditorElementProperty);
set => SetValue(EditorElementProperty, value);
}
public static readonly DependencyProperty IsExpandedEnabledProperty = DependencyProperty.Register(
"IsExpandedEnabled", typeof(bool), typeof(PropertyItem), new PropertyMetadata(ValueBoxes.FalseBox));
public bool IsExpandedEnabled
{
get => (bool) GetValue(IsExpandedEnabledProperty);
set => SetValue(IsExpandedEnabledProperty, ValueBoxes.BooleanBox(value));
}
public PropertyDescriptor PropertyDescriptor { get; set; }
public virtual void InitElement()
{
if (Editor == null) return;
EditorElement = Editor.CreateElement(this);
Editor.CreateBinding(this, EditorElement);
}
get => GetValue(ValueProperty);
set => SetValue(ValueProperty, value);
}
}
public static readonly DependencyProperty DisplayNameProperty = DependencyProperty.Register(
"DisplayName", typeof(string), typeof(PropertyItem), new PropertyMetadata(default(string)));
public string DisplayName
{
get => (string) GetValue(DisplayNameProperty);
set => SetValue(DisplayNameProperty, value);
}
public static readonly DependencyProperty PropertyNameProperty = DependencyProperty.Register(
"PropertyName", typeof(string), typeof(PropertyItem), new PropertyMetadata(default(string)));
public string PropertyName
{
get => (string) GetValue(PropertyNameProperty);
set => SetValue(PropertyNameProperty, value);
}
public static readonly DependencyProperty PropertyTypeProperty = DependencyProperty.Register(
"PropertyType", typeof(Type), typeof(PropertyItem), new PropertyMetadata(default(Type)));
public Type PropertyType
{
get => (Type) GetValue(PropertyTypeProperty);
set => SetValue(PropertyTypeProperty, value);
}
public static readonly DependencyProperty PropertyTypeNameProperty = DependencyProperty.Register(
"PropertyTypeName", typeof(string), typeof(PropertyItem), new PropertyMetadata(default(string)));
public string PropertyTypeName
{
get => (string) GetValue(PropertyTypeNameProperty);
set => SetValue(PropertyTypeNameProperty, value);
}
public static readonly DependencyProperty DescriptionProperty = DependencyProperty.Register(
"Description", typeof(string), typeof(PropertyItem), new PropertyMetadata(default(string)));
public string Description
{
get => (string) GetValue(DescriptionProperty);
set => SetValue(DescriptionProperty, value);
}
public static readonly DependencyProperty IsReadOnlyProperty = DependencyProperty.Register(
"IsReadOnly", typeof(bool), typeof(PropertyItem), new PropertyMetadata(ValueBoxes.FalseBox));
public bool IsReadOnly
{
get => (bool) GetValue(IsReadOnlyProperty);
set => SetValue(IsReadOnlyProperty, ValueBoxes.BooleanBox(value));
}
public static readonly DependencyProperty DefaultValueProperty = DependencyProperty.Register(
"DefaultValue", typeof(object), typeof(PropertyItem), new PropertyMetadata(default(object)));
public object DefaultValue
{
get => GetValue(DefaultValueProperty);
set => SetValue(DefaultValueProperty, value);
}
public static readonly DependencyProperty CategoryProperty = DependencyProperty.Register(
"Category", typeof(string), typeof(PropertyItem), new PropertyMetadata(default(string)));
public string Category
{
get => (string) GetValue(CategoryProperty);
set => SetValue(CategoryProperty, value);
}
public static readonly DependencyProperty EditorProperty = DependencyProperty.Register(
"Editor", typeof(PropertyEditorBase), typeof(PropertyItem), new PropertyMetadata(default(PropertyEditorBase)));
public PropertyEditorBase Editor
{
get => (PropertyEditorBase) GetValue(EditorProperty);
set => SetValue(EditorProperty, value);
}
public static readonly DependencyProperty EditorElementProperty = DependencyProperty.Register(
"EditorElement", typeof(FrameworkElement), typeof(PropertyItem), new PropertyMetadata(default(FrameworkElement)));
public FrameworkElement EditorElement
{
get => (FrameworkElement) GetValue(EditorElementProperty);
set => SetValue(EditorElementProperty, value);
}
public static readonly DependencyProperty IsExpandedEnabledProperty = DependencyProperty.Register(
"IsExpandedEnabled", typeof(bool), typeof(PropertyItem), new PropertyMetadata(ValueBoxes.FalseBox));
public bool IsExpandedEnabled
{
get => (bool) GetValue(IsExpandedEnabledProperty);
set => SetValue(IsExpandedEnabledProperty, ValueBoxes.BooleanBox(value));
}
public PropertyDescriptor PropertyDescriptor { get; set; }
public virtual void InitElement()
{
if (Editor == null) return;
EditorElement = Editor.CreateElement(this);
Editor.CreateBinding(this, EditorElement);
}
}
@@ -1,17 +1,16 @@
using System.Windows.Controls;
namespace HandyControl.Controls
{
public class PropertyItemsControl : ListBox
{
protected override bool IsItemItsOwnContainerOverride(object item) => item is PropertyItem;
namespace HandyControl.Controls;
public PropertyItemsControl()
{
public class PropertyItemsControl : ListBox
{
protected override bool IsItemItsOwnContainerOverride(object item) => item is PropertyItem;
public PropertyItemsControl()
{
#if !NET40
VirtualizingPanel.SetIsVirtualizingWhenGrouping(this, true);
VirtualizingPanel.SetScrollUnit(this, ScrollUnit.Pixel);
#endif
}
}
}
}
@@ -6,122 +6,121 @@ using System.Windows;
using System.Windows.Media;
using HandyControl.Properties.Langs;
namespace HandyControl.Controls
namespace HandyControl.Controls;
public class PropertyResolver
{
public class PropertyResolver
private static readonly Dictionary<Type, EditorTypeCode> TypeCodeDic = new()
{
private static readonly Dictionary<Type, EditorTypeCode> TypeCodeDic = new()
{
[typeof(string)] = EditorTypeCode.PlainText,
[typeof(sbyte)] = EditorTypeCode.SByteNumber,
[typeof(byte)] = EditorTypeCode.ByteNumber,
[typeof(short)] = EditorTypeCode.Int16Number,
[typeof(ushort)] = EditorTypeCode.UInt16Number,
[typeof(int)] = EditorTypeCode.Int32Number,
[typeof(uint)] = EditorTypeCode.UInt32Number,
[typeof(long)] = EditorTypeCode.Int64Number,
[typeof(ulong)] = EditorTypeCode.UInt64Number,
[typeof(float)] = EditorTypeCode.SingleNumber,
[typeof(double)] = EditorTypeCode.DoubleNumber,
[typeof(bool)] = EditorTypeCode.Switch,
[typeof(DateTime)] = EditorTypeCode.DateTime,
[typeof(HorizontalAlignment)] = EditorTypeCode.HorizontalAlignment,
[typeof(VerticalAlignment)] = EditorTypeCode.VerticalAlignment,
[typeof(ImageSource)] = EditorTypeCode.ImageSource
};
[typeof(string)] = EditorTypeCode.PlainText,
[typeof(sbyte)] = EditorTypeCode.SByteNumber,
[typeof(byte)] = EditorTypeCode.ByteNumber,
[typeof(short)] = EditorTypeCode.Int16Number,
[typeof(ushort)] = EditorTypeCode.UInt16Number,
[typeof(int)] = EditorTypeCode.Int32Number,
[typeof(uint)] = EditorTypeCode.UInt32Number,
[typeof(long)] = EditorTypeCode.Int64Number,
[typeof(ulong)] = EditorTypeCode.UInt64Number,
[typeof(float)] = EditorTypeCode.SingleNumber,
[typeof(double)] = EditorTypeCode.DoubleNumber,
[typeof(bool)] = EditorTypeCode.Switch,
[typeof(DateTime)] = EditorTypeCode.DateTime,
[typeof(HorizontalAlignment)] = EditorTypeCode.HorizontalAlignment,
[typeof(VerticalAlignment)] = EditorTypeCode.VerticalAlignment,
[typeof(ImageSource)] = EditorTypeCode.ImageSource
};
public string ResolveCategory(PropertyDescriptor propertyDescriptor)
{
var categoryAttribute = propertyDescriptor.Attributes.OfType<CategoryAttribute>().FirstOrDefault();
public string ResolveCategory(PropertyDescriptor propertyDescriptor)
{
var categoryAttribute = propertyDescriptor.Attributes.OfType<CategoryAttribute>().FirstOrDefault();
return categoryAttribute == null ?
return categoryAttribute == null ?
Lang.Miscellaneous :
string.IsNullOrEmpty(categoryAttribute.Category) ?
Lang.Miscellaneous :
string.IsNullOrEmpty(categoryAttribute.Category) ?
Lang.Miscellaneous :
categoryAttribute.Category;
}
public string ResolveDisplayName(PropertyDescriptor propertyDescriptor)
{
var displayName = propertyDescriptor.DisplayName;
if (string.IsNullOrEmpty(displayName))
{
displayName = propertyDescriptor.Name;
}
return displayName;
}
public string ResolveDescription(PropertyDescriptor propertyDescriptor) => propertyDescriptor.Description;
public bool ResolveIsBrowsable(PropertyDescriptor propertyDescriptor) => propertyDescriptor.IsBrowsable;
public bool ResolveIsDisplay(PropertyDescriptor propertyDescriptor) => propertyDescriptor.IsLocalizable;
public bool ResolveIsReadOnly(PropertyDescriptor propertyDescriptor) => propertyDescriptor.IsReadOnly;
public object ResolveDefaultValue(PropertyDescriptor propertyDescriptor)
{
var defaultValueAttribute = propertyDescriptor.Attributes.OfType<DefaultValueAttribute>().FirstOrDefault();
return defaultValueAttribute?.Value;
}
public PropertyEditorBase ResolveEditor(PropertyDescriptor propertyDescriptor)
{
var editorAttribute = propertyDescriptor.Attributes.OfType<EditorAttribute>().FirstOrDefault();
var editor = editorAttribute == null || string.IsNullOrEmpty(editorAttribute.EditorTypeName)
? CreateDefaultEditor(propertyDescriptor.PropertyType)
: CreateEditor(Type.GetType(editorAttribute.EditorTypeName));
return editor;
}
public virtual PropertyEditorBase CreateDefaultEditor(Type type) =>
TypeCodeDic.TryGetValue(type, out var editorType)
? editorType switch
{
EditorTypeCode.PlainText => new PlainTextPropertyEditor(),
EditorTypeCode.SByteNumber => new NumberPropertyEditor(sbyte.MinValue, sbyte.MaxValue),
EditorTypeCode.ByteNumber => new NumberPropertyEditor(byte.MinValue, byte.MaxValue),
EditorTypeCode.Int16Number => new NumberPropertyEditor(short.MinValue, short.MaxValue),
EditorTypeCode.UInt16Number => new NumberPropertyEditor(ushort.MinValue, ushort.MaxValue),
EditorTypeCode.Int32Number => new NumberPropertyEditor(int.MinValue, int.MaxValue),
EditorTypeCode.UInt32Number => new NumberPropertyEditor(uint.MinValue, uint.MaxValue),
EditorTypeCode.Int64Number => new NumberPropertyEditor(long.MinValue, long.MaxValue),
EditorTypeCode.UInt64Number => new NumberPropertyEditor(ulong.MinValue, ulong.MaxValue),
EditorTypeCode.SingleNumber => new NumberPropertyEditor(float.MinValue, float.MaxValue),
EditorTypeCode.DoubleNumber => new NumberPropertyEditor(double.MinValue, double.MaxValue),
EditorTypeCode.Switch => new SwitchPropertyEditor(),
EditorTypeCode.DateTime => new DateTimePropertyEditor(),
EditorTypeCode.HorizontalAlignment => new HorizontalAlignmentPropertyEditor(),
EditorTypeCode.VerticalAlignment => new VerticalAlignmentPropertyEditor(),
EditorTypeCode.ImageSource => new ImagePropertyEditor(),
_ => new ReadOnlyTextPropertyEditor()
}
: type.IsSubclassOf(typeof(Enum))
? new EnumPropertyEditor()
: new ReadOnlyTextPropertyEditor();
public virtual PropertyEditorBase CreateEditor(Type type) => Activator.CreateInstance(type) as PropertyEditorBase ?? new ReadOnlyTextPropertyEditor();
private enum EditorTypeCode
{
PlainText,
SByteNumber,
ByteNumber,
Int16Number,
UInt16Number,
Int32Number,
UInt32Number,
Int64Number,
UInt64Number,
SingleNumber,
DoubleNumber,
Switch,
DateTime,
HorizontalAlignment,
VerticalAlignment,
ImageSource
}
categoryAttribute.Category;
}
}
public string ResolveDisplayName(PropertyDescriptor propertyDescriptor)
{
var displayName = propertyDescriptor.DisplayName;
if (string.IsNullOrEmpty(displayName))
{
displayName = propertyDescriptor.Name;
}
return displayName;
}
public string ResolveDescription(PropertyDescriptor propertyDescriptor) => propertyDescriptor.Description;
public bool ResolveIsBrowsable(PropertyDescriptor propertyDescriptor) => propertyDescriptor.IsBrowsable;
public bool ResolveIsDisplay(PropertyDescriptor propertyDescriptor) => propertyDescriptor.IsLocalizable;
public bool ResolveIsReadOnly(PropertyDescriptor propertyDescriptor) => propertyDescriptor.IsReadOnly;
public object ResolveDefaultValue(PropertyDescriptor propertyDescriptor)
{
var defaultValueAttribute = propertyDescriptor.Attributes.OfType<DefaultValueAttribute>().FirstOrDefault();
return defaultValueAttribute?.Value;
}
public PropertyEditorBase ResolveEditor(PropertyDescriptor propertyDescriptor)
{
var editorAttribute = propertyDescriptor.Attributes.OfType<EditorAttribute>().FirstOrDefault();
var editor = editorAttribute == null || string.IsNullOrEmpty(editorAttribute.EditorTypeName)
? CreateDefaultEditor(propertyDescriptor.PropertyType)
: CreateEditor(Type.GetType(editorAttribute.EditorTypeName));
return editor;
}
public virtual PropertyEditorBase CreateDefaultEditor(Type type) =>
TypeCodeDic.TryGetValue(type, out var editorType)
? editorType switch
{
EditorTypeCode.PlainText => new PlainTextPropertyEditor(),
EditorTypeCode.SByteNumber => new NumberPropertyEditor(sbyte.MinValue, sbyte.MaxValue),
EditorTypeCode.ByteNumber => new NumberPropertyEditor(byte.MinValue, byte.MaxValue),
EditorTypeCode.Int16Number => new NumberPropertyEditor(short.MinValue, short.MaxValue),
EditorTypeCode.UInt16Number => new NumberPropertyEditor(ushort.MinValue, ushort.MaxValue),
EditorTypeCode.Int32Number => new NumberPropertyEditor(int.MinValue, int.MaxValue),
EditorTypeCode.UInt32Number => new NumberPropertyEditor(uint.MinValue, uint.MaxValue),
EditorTypeCode.Int64Number => new NumberPropertyEditor(long.MinValue, long.MaxValue),
EditorTypeCode.UInt64Number => new NumberPropertyEditor(ulong.MinValue, ulong.MaxValue),
EditorTypeCode.SingleNumber => new NumberPropertyEditor(float.MinValue, float.MaxValue),
EditorTypeCode.DoubleNumber => new NumberPropertyEditor(double.MinValue, double.MaxValue),
EditorTypeCode.Switch => new SwitchPropertyEditor(),
EditorTypeCode.DateTime => new DateTimePropertyEditor(),
EditorTypeCode.HorizontalAlignment => new HorizontalAlignmentPropertyEditor(),
EditorTypeCode.VerticalAlignment => new VerticalAlignmentPropertyEditor(),
EditorTypeCode.ImageSource => new ImagePropertyEditor(),
_ => new ReadOnlyTextPropertyEditor()
}
: type.IsSubclassOf(typeof(Enum))
? new EnumPropertyEditor()
: new ReadOnlyTextPropertyEditor();
public virtual PropertyEditorBase CreateEditor(Type type) => Activator.CreateInstance(type) as PropertyEditorBase ?? new ReadOnlyTextPropertyEditor();
private enum EditorTypeCode
{
PlainText,
SByteNumber,
ByteNumber,
Int16Number,
UInt16Number,
Int32Number,
UInt32Number,
Int64Number,
UInt64Number,
SingleNumber,
DoubleNumber,
Switch,
DateTime,
HorizontalAlignment,
VerticalAlignment,
ImageSource
}
}