简体   繁体   English

使用Linq to XML生成动态WPF

[英]Dynamic WPF generation with Linq to XML

I have been trying to create some dynamic Xaml. 我一直在尝试创建一些动态Xaml。

I have the following c# 我有以下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. 我得到的错误是尝试创建Grid.Column。 The exact error is {"Der unbekannte Member \\"Grid.Column\\" kann nicht festgelegt werden."} 确切的错误是{“Der unbekannte Member \\”Grid.Column \\“kann nicht festgelegt werden。”}

so it doesn't know the Grid.Column... 所以它不知道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! 它适用于新的XAttribute(“Grid.Column”,“1”),注释掉的行只是没有显示我想要的东西!

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. 似乎XamlReader.Load与提供的XmlReader的交互存在一些问题。 It throws XamlParseException "Cannot set unknown member 'Grid.Column'." 它抛出XamlParseException “无法设置未知成员'Grid.Column'。”

Converting to string and loading XAML from it works, oddly: 转换为字符串并从中加载XAML很有效,奇怪的是:

var xml = UI.ToString();
var reader = XmlReader.Create(new StringReader(xml));
var content = XamlReader.Load(reader); // works now.

I see you're creating XML markup only to convert it to UI objects. 我看到你创建XML标记只是为了将其转换为UI对象。 Why not simply create UI objects from the queries? 为什么不简单地从查询中创建UI对象?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM