[英]Dynamic WPF generation with Linq to XML
我一直在嘗試創建一些動態Xaml。
我有以下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。 確切的錯誤是{“Der unbekannte Member \\”Grid.Column \\“kann 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與提供的XmlReader的交互存在一些問題。 它拋出XamlParseException “無法設置未知成員'Grid.Column'。”
轉換為字符串並從中加載XAML很有效,奇怪的是:
var xml = UI.ToString();
var reader = XmlReader.Create(new StringReader(xml));
var content = XamlReader.Load(reader); // works now.
我看到你創建XML標記只是為了將其轉換為UI對象。 為什么不簡單地從查詢中創建UI對象?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.