Here's what I'm trying to do:
XAML:
<local:MegaGrid MegaCol="auto,auto,*,auto">
<Button Grid.Column="0" Content="Btn1"/>
<Button Grid.Column="1" Content="Btn2"/>
<Button Grid.Column="2" Content="Btn3 Stretch"
HorizontalAlignment="Stretch"/>
<Button Grid.Column="3" Content="Btn2"/>
</local:MegaGrid>
C#:
//All the Normal using statements plus a few more...
using System.Text.RegularExpressions;
//Uncomment these two for WPF or comment out for UWP
//using System.Windows;
//using System.Windows.Controls;
namespace NameSpaceOfYourApp {
public class MegaGrid : Grid
{
private string zMegaRow = "";
public string MegaRow
{
get { return zMegaRow; }
set
{
zMegaRow = value;
RowDefinitions.Clear();
string value2 = Regex.Replace(value, @"\s+", "");
string[] items = value2.Split(',');
foreach (string item in items)
{
// QUESTION: HOW TO CONVERT ITEM
// DIRECTLY FROM STRING INTO RowDefinition?
// Without Parsing the string
if (item == "*")
{
RowDefinitions.Add(
new RowDefinition {
Height = new GridLength(1, GridUnitType.Star) }
);
}
else if (item == "auto")
{
RowDefinitions.Add(
new RowDefinition { Height = GridLength.Auto }
);
}
}
}
} // MegaRow
private string zMegaCol = "";
public string MegaCol
{
get { return zMegaCol; }
set
{
zMegaRow = value;
ColumnDefinitions.Clear();
string value2 = Regex.Replace(value, @"\s+", "");
string[] items = value2.Split(',');
foreach (string item in items)
{
// QUESTION: HOW TO CONVERT ITEM
// DIRECTLY FROM STRING INTO ColumnDefinition?
// Without Parsing the string
if (item == "*")
{
ColumnDefinitions.Add(
new ColumnDefinition {
Width = new GridLength(1, GridUnitType.Star) }
);
}
else if (item == "auto")
{
ColumnDefinitions.Add(
new ColumnDefinition { Width = GridLength.Auto }
);
}
}
}
} // MegaCol
} //MegaGrid
} //NameSpaceOfYourApp
What I need to know, is how to call the same String to RowDefinitions converter that the XAML uses to create the RowDefintions object. And the same for ColumnDefinitions. Except to invoke it from C# instead of from XAML. I can easily write if else statements and regex to parse the strings that XAML will accept for RowDefinition and ColumnDefinitions. I was just trying to use the functionals already built into Grid component that XAML invokes when converting from a string into these objects.