将XAML转换为c#

时间:2011-08-03 17:41:19

标签: c# wpf xaml

我需要将以下代码转换为c#代码。 有一个工具可以自动完成吗? 谢谢

 <ListView Name="listView" Margin="0,0,0,164">
        <ListView.View>
            <GridView>
                <GridView.Columns>
                    <GridViewColumn Header="Devise" DisplayMemberBinding="{Binding Path=devise}" Width="80" />
                    <GridViewColumn Header="Libelle" DisplayMemberBinding="{Binding Path=label}" Width="120" />
                    <GridViewColumn Header="Unite" DisplayMemberBinding="{Binding Path=unite}" Width="80" />
                    <GridViewColumn Header="Achat" DisplayMemberBinding="{Binding Path=achatBanque}" Width="80" />
                    <GridViewColumn Header="Vente" DisplayMemberBinding="{Binding Path=venteBanque}" Width="80" />    
                </GridView.Columns>
            </GridView>
        </ListView.View>
    </ListView>

2 个答案:

答案 0 :(得分:7)

我认为这样做不应该那么难(使用CodeDOM),所以我开始研究解决方案。来源可用at github。它只是一个开始,它不支持XAML的许多功能,但它适用于问题中的代码。

如果您对此很认真,可以进一步处理该代码。

问题中的XAML产生以下输出,似乎有效:

private ListView Get()
{
    ListView listView = new ListView();
    listView.Name = "listView";
    listView.Margin = ((Thickness)(TypeDescriptor.GetConverter(typeof(Thickness)).ConvertFromInvariantString("0,0,0,164")));
    GridView gridView = new GridView();
    GridViewColumn gridViewColumn = new GridViewColumn();
    gridViewColumn.Header = "Devise";
    Binding binding = new Binding();
    binding.Path = ((PropertyPath)(TypeDescriptor.GetConverter(typeof(PropertyPath)).ConvertFromInvariantString("devise")));
    gridViewColumn.DisplayMemberBinding = binding;
    gridViewColumn.Width = 80D;
    gridView.Columns.Add(gridViewColumn);
    GridViewColumn gridViewColumn2 = new GridViewColumn();
    gridViewColumn2.Header = "Libelle";
    Binding binding2 = new Binding();
    binding2.Path = ((PropertyPath)(TypeDescriptor.GetConverter(typeof(PropertyPath)).ConvertFromInvariantString("label")));
    gridViewColumn2.DisplayMemberBinding = binding2;
    gridViewColumn2.Width = 120D;
    gridView.Columns.Add(gridViewColumn2);
    GridViewColumn gridViewColumn3 = new GridViewColumn();
    gridViewColumn3.Header = "Unite";
    Binding binding3 = new Binding();
    binding3.Path = ((PropertyPath)(TypeDescriptor.GetConverter(typeof(PropertyPath)).ConvertFromInvariantString("unite")));
    gridViewColumn3.DisplayMemberBinding = binding3;
    gridViewColumn3.Width = 80D;
    gridView.Columns.Add(gridViewColumn3);
    GridViewColumn gridViewColumn4 = new GridViewColumn();
    gridViewColumn4.Header = "Achat";
    Binding binding4 = new Binding();
    binding4.Path = ((PropertyPath)(TypeDescriptor.GetConverter(typeof(PropertyPath)).ConvertFromInvariantString("achatBanque")));
    gridViewColumn4.DisplayMemberBinding = binding4;
    gridViewColumn4.Width = 80D;
    gridView.Columns.Add(gridViewColumn4);
    GridViewColumn gridViewColumn5 = new GridViewColumn();
    gridViewColumn5.Header = "Vente";
    Binding binding5 = new Binding();
    binding5.Path = ((PropertyPath)(TypeDescriptor.GetConverter(typeof(PropertyPath)).ConvertFromInvariantString("venteBanque")));
    gridViewColumn5.DisplayMemberBinding = binding5;
    gridViewColumn5.Width = 80D;
    gridView.Columns.Add(gridViewColumn5);
    listView.View = gridView;
    return listView;
}

答案 1 :(得分:1)

没有工具可以自动执行此操作,但通常您可以获取属性并将其更改为设置属性。您可能会丢失某些类型的自动转换,但在这些情况下,您只需要确定属性的类型并使用它(例如下面的Margin)。

例如:

var gridView = new GridView();

gridView.Columns.Add(new GridViewColumn() {
    Header = "Devise",
    DisplayMemberBinding = new Binding() { Path = "devise" },
    Width = 80
});
// ...

var listView = new ListView() {
    Name = "listView",
    Margin = new Thickness(0, 0, 0, 164),
    View = gridView
};