Skip to content
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

type convertor xaml shortening #1

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5036,6 +5036,24 @@ public void Refresh() { }
public override bool ShouldSerializeContent() { throw null; }
public void StopLoading() { }
}
public partial class ColumnDefinitionCollectionConverter : System.ComponentModel.TypeConverter
{
public ColumnDefinitionCollectionConverter() { }
public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext typeDescriptorContext, System.Type sourceType) { throw null; }
public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type destinationType) { throw null; }
public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext typeDescriptorContext, System.Globalization.CultureInfo cultureInfo, object source) { throw null; }
public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) { throw null; }
}

public partial class RowDefinitionCollectionConverter : System.ComponentModel.TypeConverter
{
public RowDefinitionCollectionConverter() { }
public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext typeDescriptorContext, System.Type sourceType) { throw null; }
public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type destinationType) { throw null; }
public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext typeDescriptorContext, System.Globalization.CultureInfo cultureInfo, object source) { throw null; }
public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) { throw null; }
}

public partial class Grid : System.Windows.Controls.Panel, System.Windows.Markup.IAddChild
{
public static readonly System.Windows.DependencyProperty ColumnProperty;
Expand All @@ -5045,9 +5063,11 @@ public partial class Grid : System.Windows.Controls.Panel, System.Windows.Markup
public static readonly System.Windows.DependencyProperty RowSpanProperty;
public static readonly System.Windows.DependencyProperty ShowGridLinesProperty;
public Grid() { }
[System.ComponentModel.TypeConverterAttribute(typeof(System.Windows.Controls.ColumnDefinitionCollectionConverter))]
[System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Content)]
public System.Windows.Controls.ColumnDefinitionCollection ColumnDefinitions { get { throw null; } }
protected internal override System.Collections.IEnumerator LogicalChildren { get { throw null; } }
[System.ComponentModel.TypeConverterAttribute(typeof(System.Windows.Controls.RowDefinitionCollectionConverter))]
[System.ComponentModel.DesignerSerializationVisibilityAttribute(System.ComponentModel.DesignerSerializationVisibility.Content)]
public System.Windows.Controls.RowDefinitionCollection RowDefinitions { get { throw null; } }
public bool ShowGridLines { get { throw null; } set { } }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,7 @@
<Compile Include="System\Windows\Controls\CleanUpVirtualizedItemEventArgs.cs" />
<Compile Include="System\Windows\Controls\ClickMode.cs" />
<Compile Include="System\Windows\Controls\ColumnDefinition.cs" />
<Compile Include="System\Windows\Controls\ColumnDefinitionCollectionConverter.cs" />
<Compile Include="System\Windows\Controls\ComboBox.cs" />
<Compile Include="System\Windows\Controls\ComboBoxItem.cs" />
<Compile Include="System\Windows\Controls\ContainerTracking.cs" />
Expand Down Expand Up @@ -739,6 +740,7 @@
<Compile Include="System\Windows\Controls\RealizedColumnsBlock.cs" />
<Compile Include="System\Windows\Controls\RichTextBox.cs" />
<Compile Include="System\Windows\Controls\RowDefinition.cs" />
<Compile Include="System\Windows\Controls\RowDefinitionCollectionConverter.cs" />
<Compile Include="System\Windows\Controls\ScrollChangedEventArgs.cs" />
<Compile Include="System\Windows\Controls\ScrollUnit.cs" />
<Compile Include="System\Windows\Controls\ScrollViewer.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ private void PrivateValidateValueForAddition(object value)
throw new ArgumentException(SR.Format(SR.GridCollection_MustBeCertainType, "ColumnDefinitionCollection", "ColumnDefinition"));
}

if (item.Parent != null)
if (item.Parent != _owner && item.Parent != null)
{
throw new ArgumentException(SR.Format(SR.GridCollection_InOtherCollection, "value", "ColumnDefinitionCollection"));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// 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;
himgoyalmicro marked this conversation as resolved.
Show resolved Hide resolved

#pragma warning disable 1634, 1691 // suppressing PreSharp warnings

namespace System.Windows.Controls
{

public class ColumnDefinitionCollectionConverter : TypeConverter
{
#region Public Methods

/// <summary>
/// CanConvertFrom - Returns whether or not this class can convert from a given type.
/// </summary>
/// <returns>
/// bool - True if thie converter can convert from the provided type, false if not.
/// </returns>
/// <param name="context"> The ITypeDescriptorContext for this call. </param>
/// <param name="sourceType"> The Type being queried for support. </param>
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
}

/// <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="context"> The ITypeDescriptorContext for this call. </param>
/// <param name="destinationType"> The Type being queried for support. </param>
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
return destinationType == typeof(string) || base.CanConvertTo(context, destinationType);
}

/// <summary>
/// ConvertFrom - Attempt to convert to a ColumnDefinitionCollection from the given object.
/// </summary>
/// <returns>
/// The object which was constructoed.
/// </returns>
/// <exception cref="ArgumentNullException">
/// An ArgumentNullException is thrown if the example object is null.
/// </exception>
/// <param name="context"> The ITypeDescriptorContext for this call. </param>
/// <param name="culture"> The CultureInfo which is respected when converting. </param>
/// <param name="value"> The Thickness to convert. </param>
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if(value != null)
{
if (value is string input)
{
IProvideValueTarget ipvt = context?.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
Grid grid = ipvt?.TargetObject as Grid;
if(grid != null)
{
var collection = new ColumnDefinitionCollection(grid); // Pass Grid instance
var converter = new GridLengthConverter();

if(input == ""){
return collection;
}
foreach (var length in input.Split(','))
{
if (converter.ConvertFromString(length.Trim()) is GridLength gridLength)
{
collection.Add(new ColumnDefinition { Width = gridLength });
}
}
return collection;
}
}
return base.ConvertFrom(context, culture, value);
}
throw GetConvertFromException(value);
}

/// <summary>
/// ConvertTo - Attempt to convert a ColumnDefinitionCollection to the given type
/// </summary>
/// <returns>
/// The object which was constructoed.
/// </returns>
/// <exception cref="ArgumentNullException">
/// An ArgumentNullException is thrown if the example object is null.
/// </exception>
/// <param name="context"> The ITypeDescriptorContext for this call. </param>
/// <param name="culture"> The CultureInfo which is respected when converting. </param>
/// <param name="value"> The ColumnDefintionCollection to convert. </param>
/// <param name="destinationType">The type to which to convert the ColumnDefintionCollection instance. </param>
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
ArgumentNullException.ThrowIfNull(value);
ArgumentNullException.ThrowIfNull(destinationType);
if (destinationType == typeof(string) && value is ColumnDefinitionCollection columnDefinitions)
{
var parts = new string[columnDefinitions.Count];

for (int i = 0; i < columnDefinitions.Count; i++)
{
parts[i] = columnDefinitions[i].Width.ToString();
}

return string.Join(",", parts);
}

return base.ConvertTo(context, culture, value, destinationType);
}

#endregion Public Methods
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Markup;

#pragma warning disable 1634, 1691 // suppressing PreSharp warnings

namespace System.Windows.Controls
Expand Down Expand Up @@ -272,6 +271,7 @@ public bool ShowGridLines
/// <summary>
/// Returns a ColumnDefinitionCollection of column definitions.
/// </summary>
himgoyalmicro marked this conversation as resolved.
Show resolved Hide resolved
[TypeConverter(typeof(ColumnDefinitionCollectionConverter))]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public ColumnDefinitionCollection ColumnDefinitions
{
Expand All @@ -282,11 +282,21 @@ public ColumnDefinitionCollection ColumnDefinitions

return (_data.ColumnDefinitions);
}
set
{
if (value == null){
_data.ColumnDefinitions = new ColumnDefinitionCollection(this);
return;
}
_data ??= new ExtendedData();
_data.ColumnDefinitions = value;
}
}

/// <summary>
/// Returns a RowDefinitionCollection of row definitions.
/// </summary>
[TypeConverter(typeof(RowDefinitionCollectionConverter))]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public RowDefinitionCollection RowDefinitions
{
Expand All @@ -297,6 +307,15 @@ public RowDefinitionCollection RowDefinitions

return (_data.RowDefinitions);
}
set
{
if (value == null){
himgoyalmicro marked this conversation as resolved.
Show resolved Hide resolved
_data.RowDefinitions = new RowDefinitionCollection(this);
return;
}
_data ??= new ExtendedData();
_data.RowDefinitions = value;
}
}

#endregion Public Properties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -562,7 +562,7 @@ private void PrivateValidateValueForAddition(object value)
throw new ArgumentException(SR.Format(SR.GridCollection_MustBeCertainType, "RowDefinitionCollection", "RowDefinition"));
}

if (item.Parent != null)
if (item.Parent != _owner && item.Parent != null)
{
throw new ArgumentException(SR.Format(SR.GridCollection_InOtherCollection, "value", "RowDefinitionCollection"));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
// 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;

#pragma warning disable 1634, 1691 // suppressing PreSharp warnings

namespace System.Windows.Controls
{

public class RowDefinitionCollectionConverter : TypeConverter
{
#region Public Methods

/// <summary>
/// CanConvertFrom - Returns whether or not this class can convert from a given type.
/// </summary>
/// <returns>
/// bool - True if thie converter can convert from the provided type, false if not.
/// </returns>
/// <param name="context"> The ITypeDescriptorContext for this call. </param>
/// <param name="sourceType"> The Type being queried for support. </param>
public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
{
return sourceType == typeof(string) || base.CanConvertFrom(context, sourceType);
}

/// <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="context"> The ITypeDescriptorContext for this call. </param>
/// <param name="destinationType"> The Type being queried for support. </param>
public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
{
return destinationType == typeof(string) || base.CanConvertTo(context, destinationType);
}

/// <summary>
/// ConvertFrom - Attempt to convert to a RowDefinitionCollection from the given object.
/// </summary>
/// <returns>
/// The object which was constructoed.
/// </returns>
/// <exception cref="ArgumentNullException">
/// An ArgumentNullException is thrown if the example object is null.
himgoyalmicro marked this conversation as resolved.
Show resolved Hide resolved
/// </exception>
/// <exception cref="ArgumentException">
/// An ArgumentException is thrown if the object is not null and is not a valid type,
/// or if the destinationType isn't one of the valid destination types.
/// </exception>
/// <param name="context"> The ITypeDescriptorContext for this call. </param>
/// <param name="culture"> The CultureInfo which is respected when converting. </param>
/// <param name="value"> The Thickness to convert. </param>
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
{
if(value != null){
if (value is string input)
{
IProvideValueTarget ipvt = context?.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
Grid grid = ipvt?.TargetObject as Grid;
if(grid != null)
{
var collection = new RowDefinitionCollection(grid); // Pass Grid instance
var converter = new GridLengthConverter();

if(input == ""){
return collection;
}
foreach (var length in input.Split(','))
{
if (converter.ConvertFromString(length.Trim()) is GridLength gridLength)
{
collection.Add(new RowDefinition { Height = gridLength });
}
}
return collection;
}
}
return base.ConvertFrom(context, culture, value);
}
throw GetConvertFromException(value);
}

/// <summary>
/// ConvertTo - Attempt to convert a RowDefinitionCollection to the given type
/// </summary>
/// <returns>
/// The object which was constructoed.
/// </returns>
/// <exception cref="ArgumentNullException">
/// An ArgumentNullException is thrown if the example object is null.
/// </exception>
/// <param name="context"> The ITypeDescriptorContext for this call. </param>
/// <param name="culture"> The CultureInfo which is respected when converting. </param>
/// <param name="value"> The RowDefintionCollection to convert. </param>
/// <param name="destinationType">The type to which to convert the RowDefintionCollection instance. </param>
public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
{
ArgumentNullException.ThrowIfNull(value);
ArgumentNullException.ThrowIfNull(destinationType);
if (destinationType == typeof(string) && value is RowDefinitionCollection RowDefinitions)
{
var parts = new string[RowDefinitions.Count];

for (int i = 0; i < RowDefinitions.Count; i++)
{
parts[i] = RowDefinitions[i].Height.ToString();
}

return string.Join(",", parts);
}

return base.ConvertTo(context, culture, value, destinationType);
}

#endregion Public Methods
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -853,6 +853,8 @@ public static Type GetKnownType(Int16 typeId)
case 757: t = () => typeof(XmlLanguageConverter); break;
case 758: t = () => typeof(XmlNamespaceMapping); break;
case 759: t = () => typeof(ZoomPercentageConverter); break;
case 760: t = () => typeof(RowDefinitionCollectionConverter); break;
case 761: t = () => typeof(ColumnDefinitionCollectionConverter); break;
default: t = () => null; break;
}

Expand Down Expand Up @@ -955,6 +957,8 @@ internal static TypeConverter CreateKnownTypeConverter(Int16 converterId)
case -722: o = new System.Windows.Media.VectorCollectionConverter(); break;
case -723: o = new System.Windows.VectorConverter(); break;
case -757: o = new System.Windows.Markup.XmlLanguageConverter(); break;
case -760: o = new System.Windows.Controls.RowDefinitionCollectionConverter(); break;
case -761: o = new System.Windows.Controls.ColumnDefinitionCollectionConverter(); break;
}
return o;
}
Expand Down
Loading