I have been trying to create some dynamic Xaml.
I have the following c#
private void LoadUI()
{
XNamespace xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation";
dynamic UI = new XElement(xmlns + "Grid",
new XAttribute(XNamespace.Xmlns + "x", "http://schemas.microsoft.com/winfx/2006/xaml"),
new XAttribute("Name", "Grid1"),
new XElement(xmlns + "Grid.ColumnDefinitions",
new XElement(xmlns + "ColumnDefinition", new XAttribute("Width", "100*")),
new XElement(xmlns + "ColumnDefinition", new XAttribute("Width", "200*"))),
new XElement(xmlns + "StackPanel", new XAttribute("Name", "StackLabels"),
new XAttribute("Margin", "3"),
from column in this.TableSchema
where column.IsPrimaryKey == 0 && column.DataType != "timestamp"
select
new XElement(xmlns + "Label", new XAttribute("Height", "28"),
new XAttribute("Name", column.ColumnName + "Label"),
new XAttribute("HorizontalContentAlignment", "Right"),
column.ColumnName)),
new XElement(xmlns + "StackPanel",
new XAttribute("Grid.Column", "1"),
new XAttribute("Name", "StackFields"),
new XAttribute("Margin", "3")
,
from column in this.TableSchema
where column.IsPrimaryKey == 0 && column.DataType != "timestamp"
select
GetUIElement(column)));
this.DynamicContent.Content = XamlReader.Load(UI.CreateReader());
}
The error i get is in trying to create the Grid.Column. The exact error is
{“Der unbekannte Member \”Grid.Column\” kann nicht festgelegt werden.”}
so it doesn’t know the Grid.Column…
Anyone any ideas?
It works fine with the new XAttribute(“Grid.Column”, “1”), line commented out just doesn’t show what I want naturally!
The generated Grid looks like the following:
<Grid xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Name="Grid1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="100*" />
<ColumnDefinition Width="200*" />
</Grid.ColumnDefinitions>
<StackPanel Name="StackLabels" Margin="3">
<Label Height="28" Name="NummerLabel" HorizontalContentAlignment="Right">Nummer</Label>
</StackPanel>
<StackPanel Grid.Column="1" Name="StackFields" Margin="3">
<TextBox Height="28" Name="txtNummer" Text="{Binding Path=Nummer}" />
</StackPanel>
</Grid>
Edit:
It is reproducible. It seems there is some problem in the interaction of XamlReader.Load with the provided XmlReader. It throws XamlParseException “Cannot set unknown member ‘Grid.Column’.”
Converting to string and loading XAML from it works, oddly:
I see you’re creating XML markup only to convert it to UI objects. Why not simply create UI objects from the queries?