From 9b46a1c2e3984552b38ffcd360490fe662004772 Mon Sep 17 00:00:00 2001 From: NaBian <836904362@qq.com> Date: Wed, 1 Jul 2020 21:37:31 +0800 Subject: [PATCH] add ImagePropertyEditor --- .../Data/Model/PropertyGridDemoModel.cs | 3 + .../Controls/PropertyGridDemoCtl.xaml | 3 + .../Controls/Other/Gravatar.cs | 10 ++- .../Editors/ImagePropertyEditor.cs | 63 +++++++++++++++++++ .../Controls/PropertyGrid/PropertyResolver.cs | 8 ++- .../HandyControl_Shared.projitems | 1 + 6 files changed, 84 insertions(+), 4 deletions(-) create mode 100644 src/Shared/HandyControl_Shared/Controls/PropertyGrid/Editors/ImagePropertyEditor.cs diff --git a/src/Shared/HandyControlDemo_Shared/Data/Model/PropertyGridDemoModel.cs b/src/Shared/HandyControlDemo_Shared/Data/Model/PropertyGridDemoModel.cs index 18adf40a..ebc634a5 100644 --- a/src/Shared/HandyControlDemo_Shared/Data/Model/PropertyGridDemoModel.cs +++ b/src/Shared/HandyControlDemo_Shared/Data/Model/PropertyGridDemoModel.cs @@ -1,5 +1,6 @@ using System.ComponentModel; using System.Windows; +using System.Windows.Media; namespace HandyControlDemo.Data { @@ -20,6 +21,8 @@ namespace HandyControlDemo.Data public HorizontalAlignment HorizontalAlignment { get; set; } public VerticalAlignment VerticalAlignment { get; set; } + + public ImageSource ImageSource { get; set; } } public enum Gender diff --git a/src/Shared/HandyControlDemo_Shared/UserControl/Controls/PropertyGridDemoCtl.xaml b/src/Shared/HandyControlDemo_Shared/UserControl/Controls/PropertyGridDemoCtl.xaml index 7a939aa5..f803c23e 100644 --- a/src/Shared/HandyControlDemo_Shared/UserControl/Controls/PropertyGridDemoCtl.xaml +++ b/src/Shared/HandyControlDemo_Shared/UserControl/Controls/PropertyGridDemoCtl.xaml @@ -18,6 +18,9 @@ + + + diff --git a/src/Shared/HandyControl_Shared/Controls/Other/Gravatar.cs b/src/Shared/HandyControl_Shared/Controls/Other/Gravatar.cs index 1ba9da89..e0ce3497 100644 --- a/src/Shared/HandyControl_Shared/Controls/Other/Gravatar.cs +++ b/src/Shared/HandyControl_Shared/Controls/Other/Gravatar.cs @@ -32,14 +32,20 @@ namespace HandyControl.Controls set => SetValue(IdProperty, value); } - public static readonly DependencyProperty SourceProperty = DependencyProperty.Register( "Source", typeof(ImageSource), typeof(Gravatar), new PropertyMetadata(default(ImageSource), OnSourceChanged)); private static void OnSourceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var ctl = (Gravatar)d; - ctl.Background = new ImageBrush((ImageSource)e.NewValue); + var v = (ImageSource) e.NewValue; + + ctl.Background = v != null + ? new ImageBrush(v) + { + Stretch = Stretch.UniformToFill + } + : ResourceHelper.GetResource("SecondaryRegionBrush"); } public ImageSource Source diff --git a/src/Shared/HandyControl_Shared/Controls/PropertyGrid/Editors/ImagePropertyEditor.cs b/src/Shared/HandyControl_Shared/Controls/PropertyGrid/Editors/ImagePropertyEditor.cs new file mode 100644 index 00000000..bf463959 --- /dev/null +++ b/src/Shared/HandyControl_Shared/Controls/PropertyGrid/Editors/ImagePropertyEditor.cs @@ -0,0 +1,63 @@ +using System; +using System.Windows; +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 + }; + + 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 = propertyItem.Value, + Mode = GetBindingMode(propertyItem), + UpdateSourceTrigger = GetUpdateSourceTrigger(propertyItem), + Converter = GetConverter(propertyItem) + }); + + public override DependencyProperty GetDependencyProperty() => SourceProperty; + } +} \ No newline at end of file diff --git a/src/Shared/HandyControl_Shared/Controls/PropertyGrid/PropertyResolver.cs b/src/Shared/HandyControl_Shared/Controls/PropertyGrid/PropertyResolver.cs index 9d51c4ff..f456bcf8 100644 --- a/src/Shared/HandyControl_Shared/Controls/PropertyGrid/PropertyResolver.cs +++ b/src/Shared/HandyControl_Shared/Controls/PropertyGrid/PropertyResolver.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.ComponentModel; using System.Linq; using System.Windows; +using System.Windows.Media; using HandyControl.Properties.Langs; namespace HandyControl.Controls @@ -25,7 +26,8 @@ namespace HandyControl.Controls [typeof(bool)] = EditorTypeCode.Switch, [typeof(DateTime)] = EditorTypeCode.DateTime, [typeof(HorizontalAlignment)] = EditorTypeCode.HorizontalAlignment, - [typeof(VerticalAlignment)] = EditorTypeCode.VerticalAlignment + [typeof(VerticalAlignment)] = EditorTypeCode.VerticalAlignment, + [typeof(ImageSource)] = EditorTypeCode.ImageSource }; public string ResolveCategory(PropertyDescriptor propertyDescriptor) @@ -93,6 +95,7 @@ namespace HandyControl.Controls EditorTypeCode.DateTime => new DateTimePropertyEditor(), EditorTypeCode.HorizontalAlignment => new HorizontalAlignmentPropertyEditor(), EditorTypeCode.VerticalAlignment => new VerticalAlignmentPropertyEditor(), + EditorTypeCode.ImageSource => new ImagePropertyEditor(), _ => new ReadOnlyTextPropertyEditor() } : type.IsSubclassOf(typeof(Enum)) @@ -117,7 +120,8 @@ namespace HandyControl.Controls Switch, DateTime, HorizontalAlignment, - VerticalAlignment + VerticalAlignment, + ImageSource } } } \ No newline at end of file diff --git a/src/Shared/HandyControl_Shared/HandyControl_Shared.projitems b/src/Shared/HandyControl_Shared/HandyControl_Shared.projitems index cc7d116a..e38ea129 100644 --- a/src/Shared/HandyControl_Shared/HandyControl_Shared.projitems +++ b/src/Shared/HandyControl_Shared/HandyControl_Shared.projitems @@ -42,6 +42,7 @@ +