Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

convert XAML to c#

Tags:

c#

wpf

xaml

I need to convert the followinf code to c# code. There is a tool to do it automatically? thank you

 <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>
like image 490
sami Avatar asked Aug 03 '11 17:08

sami


2 Answers

I thought doing this shouldn't be that hard (using CodeDOM), so I started working on a solution. The source is available at github. It's really just a beginning, it doesn't support many of the features of XAML, but it works for the code in the question.

If you are serious about this, you can work on that code further.

The XAML in the question produces the following output, that seems to work:

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;
}
like image 69
svick Avatar answered Sep 26 '22 19:09

svick


No there isn't a tool to do this automatically, but in general you can take attributes and change them to setting properties. You may lose the automatic conversion for certain types though, but in those cases you'd just need to determine the type of the property and use that (such as Margin below).

For example:

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
};
like image 22
CodeNaked Avatar answered Sep 26 '22 19:09

CodeNaked