-
Notifications
You must be signed in to change notification settings - Fork 0
Object writer changes to implement shorthand syntax for ColumnDefinitions and RowDefinitions #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: release/9.0
Are you sure you want to change the base?
Changes from all commits
06644a7
f46d16e
2d21483
3aef32e
9dc24a5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using System.ComponentModel; | ||
| using System.Windows.Controls; | ||
| using System.Windows.Markup; | ||
| using System.Globalization; | ||
| using System.Text; | ||
| using MS.Internal; | ||
|
|
||
| namespace System.Windows.Controls | ||
| { | ||
| internal sealed class ColumnDefinitionConverter : TypeConverter | ||
| { | ||
| #region Public Methods | ||
|
|
||
| /// <summary> | ||
| /// CanConvertFrom - Returns whether or not this class can convert from a given type. | ||
| /// </summary> | ||
| /// <returns> | ||
| /// bool - True if this converter can convert from the provided type, false if not. | ||
| /// </returns> | ||
| /// <param name="typeDescriptorContext"> The ITypeDescriptorContext for this call. </param> | ||
| /// <param name="sourceType"> The Type being queried for support. </param> | ||
| public override bool CanConvertFrom(ITypeDescriptorContext typeDescriptorContext, Type sourceType) | ||
| { | ||
| return sourceType == typeof(string); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// CanConvertTo - Returns whether or not this class can convert to a given type. | ||
| /// </summary> | ||
| /// <returns> | ||
| /// bool - True if this converter can convert to the provided type, false if not. | ||
| /// </returns> | ||
| /// <param name="typeDescriptorContext"> The ITypeDescriptorContext for this call. </param> | ||
| /// <param name="destinationType"> The Type being queried for support. </param> | ||
| public override bool CanConvertTo(ITypeDescriptorContext typeDescriptorContext, Type destinationType) | ||
| { | ||
| return destinationType == typeof(string); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// ConvertFrom - Attempt to convert to a ColumnDefinitionCollection from the given object. | ||
| /// </summary> | ||
| /// <returns> | ||
| /// The object which was constructed. | ||
| /// </returns> | ||
| /// <exception cref="ArgumentNullException"> | ||
| /// An ArgumentNullException is thrown if the example object is null. | ||
| /// </exception> | ||
| /// <param name="typeDescriptorContext"> The ITypeDescriptorContext for this call. </param> | ||
| /// <param name="cultureInfo"> The CultureInfo which is respected when converting. </param> | ||
| /// <param name="value"> The Thickness to convert. </param> | ||
| public override object ConvertFrom(ITypeDescriptorContext typeDescriptorContext, CultureInfo cultureInfo, object value) | ||
| { | ||
| if (value is string input) | ||
| { | ||
| ColumnDefinition columnDefinition = new ColumnDefinition{ Width = GridLengthConverter.FromString(input, cultureInfo) }; | ||
| return columnDefinition; | ||
| } | ||
| throw GetConvertFromException(value); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// ConvertTo - Attempt to convert a ColumnDefinitionCollection to the given type | ||
| /// </summary> | ||
| /// <returns> | ||
| /// The object which was constructed. | ||
| /// </returns> | ||
| /// <exception cref="ArgumentNullException"> | ||
| /// An ArgumentNullException is thrown if the example object is null. | ||
| /// </exception> | ||
| /// <param name="typeDescriptorContext"> The ITypeDescriptorContext for this call. </param> | ||
| /// <param name="cultureInfo"> The CultureInfo which is respected when converting. </param> | ||
| /// <param name="value"> The ColumnDefinitionCollection to convert. </param> | ||
| /// <param name="destinationType">The type to which to convert the ColumnDefinitionCollection instance. </param> | ||
| public override object ConvertTo(ITypeDescriptorContext typeDescriptorContext, CultureInfo cultureInfo, object value, Type destinationType) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(value); | ||
| ArgumentNullException.ThrowIfNull(destinationType); | ||
| if (destinationType == typeof(string) && value is ColumnDefinition columnDefinition) | ||
| { | ||
| return GridLengthConverter.ToString(columnDefinition.Width, cultureInfo); | ||
| } | ||
| throw GetConvertToException(value, destinationType); | ||
| } | ||
|
|
||
| #endregion Public Methods | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using System.ComponentModel; | ||
| using System.Windows.Controls; | ||
| using System.Windows.Markup; | ||
| using System.Globalization; | ||
| using System.Text; | ||
| using MS.Internal; | ||
|
|
||
| namespace System.Windows.Controls | ||
| { | ||
| internal sealed class RowDefinitionConverter : TypeConverter | ||
| { | ||
| #region Public Methods | ||
|
|
||
| /// <summary> | ||
| /// CanConvertFrom - Returns whether or not this class can convert from a given type. | ||
| /// </summary> | ||
| /// <returns> | ||
| /// bool - True if this converter can convert from the provided type, false if not. | ||
| /// </returns> | ||
| /// <param name="typeDescriptorContext"> The ITypeDescriptorContext for this call. </param> | ||
| /// <param name="sourceType"> The Type being queried for support. </param> | ||
| public override bool CanConvertFrom(ITypeDescriptorContext typeDescriptorContext, Type sourceType) | ||
| { | ||
| return sourceType == typeof(string); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// CanConvertTo - Returns whether or not this class can convert to a given type. | ||
| /// </summary> | ||
| /// <returns> | ||
| /// bool - True if this converter can convert to the provided type, false if not. | ||
| /// </returns> | ||
| /// <param name="typeDescriptorContext"> The ITypeDescriptorContext for this call. </param> | ||
| /// <param name="destinationType"> The Type being queried for support. </param> | ||
| public override bool CanConvertTo(ITypeDescriptorContext typeDescriptorContext, Type destinationType) | ||
| { | ||
| return destinationType == typeof(string); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// ConvertFrom - Attempt to convert to a RowDefinitionCollection from the given object. | ||
| /// </summary> | ||
| /// <returns> | ||
| /// The object which was constructed. | ||
| /// </returns> | ||
| /// <exception cref="ArgumentNullException"> | ||
| /// An ArgumentNullException is thrown if the example object is null. | ||
| /// </exception> | ||
| /// <param name="typeDescriptorContext"> The ITypeDescriptorContext for this call. </param> | ||
| /// <param name="cultureInfo"> The CultureInfo which is respected when converting. </param> | ||
| /// <param name="value"> The Thickness to convert. </param> | ||
| public override object ConvertFrom(ITypeDescriptorContext typeDescriptorContext, CultureInfo cultureInfo, object value) | ||
| { | ||
| if (value is string input) | ||
| { | ||
| RowDefinition rowDefinition = new RowDefinition{ Height = GridLengthConverter.FromString(input, cultureInfo) }; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You might be able to use type descriptor to decide whether you should return column or row and have only one converter (possibly even the grid length one), but if this ends up being public, it possibly is a better API separately |
||
| return rowDefinition; | ||
| } | ||
| throw GetConvertFromException(value); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// ConvertTo - Attempt to convert a RowDefinitionCollection to the given type | ||
| /// </summary> | ||
| /// <returns> | ||
| /// The object which was constructed. | ||
| /// </returns> | ||
| /// <exception cref="ArgumentNullException"> | ||
| /// An ArgumentNullException is thrown if the example object is null. | ||
| /// </exception> | ||
| /// <param name="typeDescriptorContext"> The ITypeDescriptorContext for this call. </param> | ||
| /// <param name="cultureInfo"> The CultureInfo which is respected when converting. </param> | ||
| /// <param name="value"> The RowDefinitionCollection to convert. </param> | ||
| /// <param name="destinationType">The type to which to convert the RowDefinitionCollection instance. </param> | ||
| public override object ConvertTo(ITypeDescriptorContext typeDescriptorContext, CultureInfo cultureInfo, object value, Type destinationType) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(value); | ||
| ArgumentNullException.ThrowIfNull(destinationType); | ||
| if (destinationType == typeof(string) && value is RowDefinition rowDefinition) | ||
| { | ||
| return GridLengthConverter.ToString(rowDefinition.Height, cultureInfo); | ||
| } | ||
| throw GetConvertToException(value, destinationType); | ||
| } | ||
|
|
||
| #endregion Public Methods | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -456,6 +456,12 @@ internal bool IsXmlDataIsland() | |
| { | ||
| return (_xmlDataIslandDepth != -1); | ||
| } | ||
|
|
||
| internal bool CanInitializeCollectionFromString(Type type) | ||
| { | ||
| return IsACollection(type) && XamlTypeMapper.GetTypeConverterType(type) == null | ||
| && XamlTypeMapper.GetTypeConverterType(GetCollectionItemType(type)) != null; | ||
| } | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What you could test for though is whether the item type converter can convert from string There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But again you need to decide whether it is desirable to throw "no setter" error or converter error. Maybe the latter is better in this case too. |
||
| #endregion internalMethods | ||
|
|
||
| #region CacheCallbacks | ||
|
|
@@ -3225,13 +3231,13 @@ private void WritePropertyAttribute( | |
| propertyCanWrite = false; | ||
| } | ||
|
|
||
| if (!propertyCanWrite) | ||
| if (!propertyCanWrite && !CanInitializeCollectionFromString(propInfo.PropertyType)) | ||
| { | ||
| ThrowExceptionWithLine(SR.Format(SR.ParserReadOnlyProp, attribLocalName)); | ||
| } | ||
| } | ||
|
|
||
| if (propInfo != null && !XamlTypeMapper.IsAllowedPropertySet(propInfo)) | ||
| if (propInfo != null && !CanInitializeCollectionFromString(propInfo.PropertyType) && !XamlTypeMapper.IsAllowedPropertySet(propInfo)) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would probably add the new condition at the end for consistency and performance |
||
| { | ||
| ThrowException(nameof(SR.ParserCantSetAttribute), "property", $"{declaringType.Name}.{attribLocalName}", "set"); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need to do more checks, you cannot express columns/rows that have min/max/offset set