add ImagePropertyEditor

This commit is contained in:
NaBian committed 2020-07-01 21:37:31 +08:00
1 parent 1e08a45a4e
commit 9b46a1c2e3
6 files changed
+84 -4

No files matched your search

@@ -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
@@ -18,6 +18,9 @@
<TextBox hc:TitleElement.Title="Boolean" hc:TitleElement.TitlePlacement="Left" Style="{StaticResource TextBoxExtend}" Text="{Binding DemoModel.Boolean,Mode=OneWay}" IsReadOnly="True" Margin="0,6,0,0"/>
<TextBox hc:TitleElement.Title="HorizontalAlignment" hc:TitleElement.TitlePlacement="Left" Style="{StaticResource TextBoxExtend}" Text="{Binding DemoModel.HorizontalAlignment,Mode=OneWay}" IsReadOnly="True" Margin="0,6,0,0"/>
<TextBox hc:TitleElement.Title="VerticalAlignment" hc:TitleElement.TitlePlacement="Left" Style="{StaticResource TextBoxExtend}" Text="{Binding DemoModel.VerticalAlignment,Mode=OneWay}" IsReadOnly="True" Margin="0,6,0,0"/>
<GroupBox Style="{StaticResource GroupBoxOriginal}" Header="ImageSource" hc:TitleElement.TitlePlacement="Left" Margin="0,6,0,0">
<hc:Gravatar HorizontalAlignment="Left" Source="{Binding DemoModel.ImageSource}"/>
</GroupBox>
</StackPanel>
</Grid>
</hc:TransitioningContentControl>
@@ -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<Brush>("SecondaryRegionBrush");
}
public ImageSource Source
@@ -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;
}
}
@@ -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
}
}
}
@@ -42,6 +42,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Controls\Other\Notification.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Other\Poptip.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\Other\Watermark.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\PropertyGrid\Editors\ImagePropertyEditor.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\PropertyGrid\Editors\VerticalAlignmentPropertyEditor.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\PropertyGrid\Editors\HorizontalAlignmentPropertyEditor.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Controls\PropertyGrid\Editors\EnumPropertyEditor.cs" />