我一直在嘗試創建一些動態Xaml。Linq to XML的動態WPF生成
我有下面的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());
}
我的錯誤是在試圖創建Grid.Column。確切的錯誤是 { 「明鏡unbekannte會員\」 Grid.Column \ 「卡恩nicht festgelegt werden。」}
所以它不知道Grid.Column ...
任何人任何想法?
它與新的XAttribute(「Grid.Column」,「1」)正常工作,行註釋只是不顯示我想要的自然!
生成網格如下所示:
<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>
而且是在XamlReader.Load或更早版本中發生的錯誤?更好地發佈完整的錯誤。 – 2012-01-18 15:47:43
對不起,錯誤在XamlReader.Load中發生。 UI中的Xaml對我來說看起來不錯... – stackeroverflow 2012-01-18 15:49:17
嘗試在VS中加載生成的xaml並讓它自動格式化。很容易錯過關閉標籤或其他東西。可以在這裏發佈(樣本)嗎? – 2012-01-18 15:52:13