added GetValueOnInstance<T> that uses the TypeConverter defined on the XamlProperty if types are not direct match, this way the user can extend the specific TypeConverter if needed.

This commit is contained in:
Luke Ordelmans 2023-03-01 10:36:12 +01:00
Родитель 21442d0296
Коммит daf315524d
7 изменённых файлов: 49 добавлений и 18 удалений

Просмотреть файл

@ -368,7 +368,7 @@ namespace ICSharpCode.WpfDesign.Designer.Controls
// increment ColSpan of all controls in the split column, increment Column of all controls in later columns:
foreach (DesignItem child in gridItem.Properties["Children"].CollectionElements) {
Point topLeft = child.View.TranslatePoint(new Point(0, 0), grid);
var margin = (Thickness) child.Properties[FrameworkElement.MarginProperty].ValueOnInstance;
var margin = child.Properties[FrameworkElement.MarginProperty].GetValueOnInstance<Thickness>();
var start = (int) child.Properties.GetAttachedProperty(idxProperty).ValueOnInstance;
var span = (int) child.Properties.GetAttachedProperty(spanProperty).ValueOnInstance;
if (start <= splitIndex && splitIndex < start + span) {
@ -395,7 +395,7 @@ namespace ICSharpCode.WpfDesign.Designer.Controls
foreach (DesignItem child in gridItem.Properties["Children"].CollectionElements)
{
Point topLeft = child.View.TranslatePoint(new Point(0, 0), grid);
var margin = (Thickness)child.Properties[FrameworkElement.MarginProperty].ValueOnInstance;
var margin = child.Properties[FrameworkElement.MarginProperty].GetValueOnInstance<Thickness>();
var start = (int)child.Properties.GetAttachedProperty(idxProperty).ValueOnInstance;
var span = (int)child.Properties.GetAttachedProperty(spanProperty).ValueOnInstance;
if (start <= splitIndex && splitIndex < start + span)

Просмотреть файл

@ -189,7 +189,7 @@ namespace ICSharpCode.WpfDesign.Designer.Extensions
if (operation.Type == PlacementType.PasteItem) {
foreach (PlacementInformation info in operation.PlacedItems) {
var margin = (Thickness)info.Item.Properties.GetProperty(FrameworkElement.MarginProperty).ValueOnInstance;
var margin = info.Item.Properties.GetProperty(FrameworkElement.MarginProperty).GetValueOnInstance<Thickness>();
var horizontalAlignment = (HorizontalAlignment)info.Item.Properties.GetProperty(FrameworkElement.HorizontalAlignmentProperty).ValueOnInstance;
var verticalAlignment = (VerticalAlignment)info.Item.Properties.GetProperty(FrameworkElement.VerticalAlignmentProperty).ValueOnInstance;

Просмотреть файл

@ -78,7 +78,7 @@ namespace ICSharpCode.WpfDesign.Designer.Extensions
var column = (int) this.ExtendedItem.Properties.GetAttachedProperty(Grid.ColumnProperty).ValueOnInstance;
var columnSpan = (int) this.ExtendedItem.Properties.GetAttachedProperty(Grid.ColumnSpanProperty).ValueOnInstance;
var margin = (Thickness) this.ExtendedItem.Properties[FrameworkElement.MarginProperty].ValueOnInstance;
var margin = this.ExtendedItem.Properties[FrameworkElement.MarginProperty].GetValueOnInstance<Thickness>();
var point = this.ExtendedItem.View.TranslatePoint(new Point(), _grid);
var position = new Rect(point, PlacementOperation.GetRealElementSize(this.ExtendedItem.View));

Просмотреть файл

@ -677,14 +677,14 @@ namespace ICSharpCode.WpfDesign.Designer
{
if ((HorizontalAlignment)item.Properties.GetProperty(FrameworkElement.HorizontalAlignmentProperty).ValueOnInstance != HorizontalAlignment.Right)
{
var margin = (Thickness)item.Properties.GetProperty(FrameworkElement.MarginProperty).ValueOnInstance;
var margin = item.Properties.GetProperty(FrameworkElement.MarginProperty).GetValueOnInstance<Thickness>();
margin.Left = xmin;
item.Properties.GetProperty(FrameworkElement.MarginProperty).SetValue(margin);
}
else
{
var pos = (double)((Panel)item.Parent.Component).ActualWidth - (xmin + (double)((FrameworkElement)item.Component).ActualWidth);
var margin = (Thickness)item.Properties.GetProperty(FrameworkElement.MarginProperty).ValueOnInstance;
var margin = item.Properties.GetProperty(FrameworkElement.MarginProperty).GetValueOnInstance<Thickness>();
margin.Right = pos;
item.Properties.GetProperty(FrameworkElement.MarginProperty).SetValue(margin);
}
@ -713,7 +713,7 @@ namespace ICSharpCode.WpfDesign.Designer
{
if ((HorizontalAlignment)item.Properties.GetProperty(FrameworkElement.HorizontalAlignmentProperty).ValueOnInstance != HorizontalAlignment.Right)
{
var margin = (Thickness)item.Properties.GetProperty(FrameworkElement.MarginProperty).ValueOnInstance;
var margin = item.Properties.GetProperty(FrameworkElement.MarginProperty).GetValueOnInstance<Thickness>();
margin.Left = mpos - (((FrameworkElement)item.Component).ActualWidth) / 2;
item.Properties.GetProperty(FrameworkElement.MarginProperty).SetValue(margin);
}
@ -721,7 +721,7 @@ namespace ICSharpCode.WpfDesign.Designer
{
var pp = mpos - (((FrameworkElement)item.Component).ActualWidth) / 2;
var pos = (double)((Panel)item.Parent.Component).ActualWidth - pp - (((FrameworkElement)item.Component).ActualWidth);
var margin = (Thickness)item.Properties.GetProperty(FrameworkElement.MarginProperty).ValueOnInstance;
var margin = item.Properties.GetProperty(FrameworkElement.MarginProperty).GetValueOnInstance<Thickness>();
margin.Right = pos;
item.Properties.GetProperty(FrameworkElement.MarginProperty).SetValue(margin);
}
@ -748,14 +748,14 @@ namespace ICSharpCode.WpfDesign.Designer
if ((HorizontalAlignment)item.Properties.GetProperty(FrameworkElement.HorizontalAlignmentProperty).ValueOnInstance != HorizontalAlignment.Right)
{
var pos = xmax - (double)((FrameworkElement)item.Component).ActualWidth;
var margin = (Thickness)item.Properties.GetProperty(FrameworkElement.MarginProperty).ValueOnInstance;
var margin = item.Properties.GetProperty(FrameworkElement.MarginProperty).GetValueOnInstance<Thickness>();
margin.Left = pos;
item.Properties.GetProperty(FrameworkElement.MarginProperty).SetValue(margin);
}
else
{
var pos = (double)((Panel)item.Parent.Component).ActualWidth - xmax;
var margin = (Thickness)item.Properties.GetProperty(FrameworkElement.MarginProperty).ValueOnInstance;
var margin = item.Properties.GetProperty(FrameworkElement.MarginProperty).GetValueOnInstance<Thickness>();
margin.Right = pos;
item.Properties.GetProperty(FrameworkElement.MarginProperty).SetValue(margin);
}
@ -781,14 +781,14 @@ namespace ICSharpCode.WpfDesign.Designer
if ((VerticalAlignment)item.Properties.GetProperty(FrameworkElement.VerticalAlignmentProperty).ValueOnInstance != VerticalAlignment.Bottom)
{
item.Properties.GetAttachedProperty(Canvas.TopProperty).SetValue(ymin);
var margin = (Thickness)item.Properties.GetProperty(FrameworkElement.MarginProperty).ValueOnInstance;
var margin = item.Properties.GetProperty(FrameworkElement.MarginProperty).GetValueOnInstance<Thickness>();
margin.Top = ymin;
item.Properties.GetProperty(FrameworkElement.MarginProperty).SetValue(margin);
}
else
{
var pos = (double)((Panel)item.Parent.Component).ActualHeight - (ymin + (double)((FrameworkElement)item.Component).ActualHeight);
var margin = (Thickness)item.Properties.GetProperty(FrameworkElement.MarginProperty).ValueOnInstance;
var margin = item.Properties.GetProperty(FrameworkElement.MarginProperty).GetValueOnInstance<Thickness>();
margin.Bottom = pos;
item.Properties.GetProperty(FrameworkElement.MarginProperty).SetValue(margin);
}
@ -814,7 +814,7 @@ namespace ICSharpCode.WpfDesign.Designer
{
if ((VerticalAlignment)item.Properties.GetProperty(FrameworkElement.VerticalAlignmentProperty).ValueOnInstance != VerticalAlignment.Bottom)
{
var margin = (Thickness)item.Properties.GetProperty(FrameworkElement.MarginProperty).ValueOnInstance;
var margin = item.Properties.GetProperty(FrameworkElement.MarginProperty).GetValueOnInstance<Thickness>();
margin.Top = ympos - (((FrameworkElement)item.Component).ActualHeight) / 2;
item.Properties.GetProperty(FrameworkElement.MarginProperty).SetValue(margin);
}
@ -822,7 +822,7 @@ namespace ICSharpCode.WpfDesign.Designer
{
var pp = mpos - (((FrameworkElement)item.Component).ActualHeight) / 2;
var pos = (double)((Panel)item.Parent.Component).ActualHeight - pp - (((FrameworkElement)item.Component).ActualHeight);
var margin = (Thickness)item.Properties.GetProperty(FrameworkElement.MarginProperty).ValueOnInstance;
var margin = item.Properties.GetProperty(FrameworkElement.MarginProperty).GetValueOnInstance<Thickness>();
margin.Bottom = pos;
item.Properties.GetProperty(FrameworkElement.MarginProperty).SetValue(margin);
}
@ -849,14 +849,14 @@ namespace ICSharpCode.WpfDesign.Designer
if ((VerticalAlignment)item.Properties.GetProperty(FrameworkElement.VerticalAlignmentProperty).ValueOnInstance != VerticalAlignment.Bottom)
{
var pos = ymax - (double)((FrameworkElement)item.Component).ActualHeight;
var margin = (Thickness)item.Properties.GetProperty(FrameworkElement.MarginProperty).ValueOnInstance;
var margin = item.Properties.GetProperty(FrameworkElement.MarginProperty).GetValueOnInstance<Thickness>();
margin.Top = pos;
item.Properties.GetProperty(FrameworkElement.MarginProperty).SetValue(margin);
}
else
{
var pos = (double)((Panel)item.Parent.Component).ActualHeight - ymax;
var margin = (Thickness)item.Properties.GetProperty(FrameworkElement.MarginProperty).ValueOnInstance;
var margin = item.Properties.GetProperty(FrameworkElement.MarginProperty).GetValueOnInstance<Thickness>();
margin.Bottom = pos;
item.Properties.GetProperty(FrameworkElement.MarginProperty).SetValue(margin);
}

Просмотреть файл

@ -269,8 +269,10 @@ namespace ICSharpCode.WpfDesign.Designer.Xaml
_property.Reset();
_designItem.NotifyPropertyChanged(this, oldValue, null);
}
}
}
public override T GetValueOnInstance<T>() => _property.GetValueOnInstance<T>();
public sealed class PropertyChangeAction : ITransactionItem
{
readonly XamlModelProperty property;

Просмотреть файл

@ -705,5 +705,28 @@ namespace ICSharpCode.WpfDesign.XamlDom
return attribute == null && element == null;
}
}*/
/// <inheritdoc />
public T GetValueOnInstance<T>()
{
var obj = ValueOnInstance;
if (obj == null)
{
return default;
}
if (obj is T typed)
{
return typed;
}
if (TypeConverter.CanConvertTo(typeof(T)))
{
return (T)TypeConverter.ConvertTo(obj, typeof(T));
}
return (T)obj;
}
}
}

Просмотреть файл

@ -131,6 +131,12 @@ namespace ICSharpCode.WpfDesign
/// </summary>
public abstract void Reset();
/// <summary>
/// Gets the value of the property on the designed instance as an instance of T.
/// If the property is not set, or does not match type T, this returns the default value.
/// </summary>
public abstract T GetValueOnInstance<T>();
/// <summary>
/// Gets the parent design item.
/// </summary>