1

In my current project I have make UI dynamically in C#. This can be a very boring and difficult task. So is there a tool that can generate C# code of XAML UI markup?

For instance, I have grid here in XAML

    <Grid  >
        <Grid.RowDefinitions>
            <RowDefinition  />
            <RowDefinition  />
        </Grid.RowDefinitions>
    </Grid>

and I want it to be converted it to

 Grid grdTextField = new Grid();
 RowDefinition firstrow = new RowDefinition();
 RowDefinition secondrow = new RowDefinition();

This is simple case but for little bit complex UI it will be difficult to make from Code behind.

EDIT:-

Actually i am getting a Json that determine how my a large Grid created. it has six to seven types of complicated views and these views can come multiple times (so has to put some logic at run time). so overall i have to make a grid on the basis of this json.

EDIT 2 :- this is part of json on the basis of this i have to make a UI

 "decimal_variables": [
            {
                "id": 1, 
                "name": "Time Taken", 
                "var_type": 2, 
                "is_required": true, 
                "default": null, 
                "max_value": "10000.000000", 
                "min_value": "0.000000", 
                "unit": "sec", 
                "score_dependency": 2
            }, 
            {
                "id": 2, 
                "name": "Number of moves", 
                "var_type": 2, 
                "is_required": true, 
                "default": null, 
                "max_value": "1000.000000", 
                "min_value": "0.000000", 
                "unit": "moves", 
                "score_dependency": 2
            }, 
            {
                "id": 3, 
                "name": "Number of hints used", 
                "var_type": 2, 
                "is_required": true, 
                "default": null, 
                "max_value": "100.000000", 
                "min_value": "0.000000", 
                "unit": "hints", 
                "score_dependency": 2
            }
        ], 
        "choice_variables": [
            {
                "choices": [
                    {
                        "id": 1, 
                        "name": "Easy", 
                        "value": "1.000000"
                    }, 
                    {
                        "id": 2, 
                        "name": "Medium", 
                        "value": "2.000000"
                    }, 
                    {
                        "id": 3, 
                        "name": "Tough", 
                        "value": "3.000000"
                    }
                ], 
                "id": 1, 
                "name": "Difficulty level", 
                "var_type": 1, 
                "is_required": true, 
                "default": null, 
                "score_dependency": 3
            }
        ], 
        "boolean_variables": [
            {
                "id": 1, 
                "name": "Is Hint Allowed", 
                "var_type": 1, 
                "default": true, 
                "score_dependency": 3
            }
        ], 
        "text_variables": [
            {
                "id": 1, 
                "name": "Instructions to student", 
                "var_type": 1, 
                "is_required": false, 
                "default": "1. Carefully do the assignment.\r\n2. Time is key in assignment score, so make sure not to waste the time.", 
                "max_length": 1000, 
                "score_dependency": 3
            }
        ], 
        "file_variables": [
            {
                "default": "media/variable-files/supermegafunbits-1024x1024.png", 
                "id": 1, 
                "name": "Image", 
                "var_type": 1, 
                "is_required": false, 
                "file_type": "image/jpeg,image/png", 
                "score_dependency": 3
            }
        ], 

now in for file variable i have to make a ui that contain a Image Control , Button to browse Image , header TextBlock, and info TextBlock (It may be complicated).so i am looking for a tool that in which if i make a UI from xaml can be converted to C# code lot of time and unnecessary coding can be saved.

loop
  • 9,002
  • 10
  • 40
  • 76
  • 2
    Have you tried http://stackoverflow.com/questions/910814/loading-xaml-at-runtime – bit Mar 11 '14 at 06:52
  • 1
    There is no need to create or manipulate UI elements in procedural code in WPF. That's what XAML is for. Post the relevant data you're using to generate the UI and a screenshot of what you need and I can tell you the proper way to do it in WPF. – Federico Berasategui Mar 11 '14 at 14:01

1 Answers1

-1

Why you need to create it in code? You can Create a DataTemplate. Create a View and a ViewModel who will give the logic of the view. In your ViewModel, you can set, get, hide, modify the View.

I suggested to read this articles:

MVVM in Depth

WPF/MVVM Quick Start Tutorial

Greetings!

soydachi
  • 851
  • 1
  • 9
  • 24
  • thanks @dachi , i understand what you want to say but the whole view is dependend on a json so can't do it in xaml. – loop Mar 11 '14 at 07:40
  • It is useful to let the person know there's a "generally accepted" path ; but its also important to answer the question on its own merit. – GettnDer Sep 18 '20 at 14:57