使用Linq-to-XML动态生成WPF

本文关键字:WPF 动态 Linq-to-XML 使用 | 更新日期: 2023-09-27 18:20:35

我一直在尝试创建一些动态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 unbkanne Member"Grid。Column"kann nicht festgelegt werden"。}

所以它不知道网格。列…

有什么想法吗?

它可以很好地使用新的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>

使用Linq-to-XML动态生成WPF

编辑:

它是可复制的。XamlReader的交互似乎存在一些问题。使用提供的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对象呢?