Adding the RadioButton in the DataGrid at runtime

2

At some point in the software, I need to populate a DataGrid in runtime. I was able to perform this operation partially. My problem is that even though I enter RadioButton.Content , it just does not display past content. Also, Group does not work in RadioButton .

Follow the code to make it easier to understand.

    public void PovoarMacros(TabControl tcMacro)
    {
        for (int i = 1; i < 11; i++)
        {
            var tab = new TabItem();
            var dg = new DataGrid();
            var nomeMacroColumn = new DataGridTemplateColumn();
            var descricaoMacroColumn = new DataGridTextColumn();
            RadioButton radio = null;

            nomeMacroColumn.Header = "Macro";
            nomeMacroColumn.Width = 438;
            nomeMacroColumn.CanUserResize = false;
            nomeMacroColumn.CanUserReorder = false;
            var template = new DataTemplate();
            var rbFramework = new FrameworkElementFactory(typeof(RadioButton));
            rbFramework.SetBinding(ToggleButton.IsCheckedProperty, new Binding("rb"));
            template.VisualTree = rbFramework;
            nomeMacroColumn.CellTemplate = template;
            nomeMacroColumn.IsReadOnly = true;

            descricaoMacroColumn.Header = "Descricao";
            descricaoMacroColumn.Width = 438;
            descricaoMacroColumn.CanUserResize = false;
            descricaoMacroColumn.CanUserReorder = false;
            descricaoMacroColumn.Binding = new Binding("descricao");
            descricaoMacroColumn.IsReadOnly = true;

            dg.Columns.Add(nomeMacroColumn);
            dg.Columns.Add(descricaoMacroColumn);

            for (int j = 0; j < vetor.getVetor(i).Length; j++)
            {
                var macro = (vetor.getVetor(i))[j];
                radio = new RadioButton();
                radio.Content = macro[0];
                radio.GroupName = vetor.getNomeVetor(i);

                dg.Items.Add(new Linha() { rb = radio, descricao = macro[1] });
            }

            tab.Header = vetor.getNomeVetor(i);
            tab.Content = dg;
            tcMacro.Items.Add(tab);
        }
    }
    
asked by anonymous 03.02.2016 / 18:36

1 answer

1

I was able to solve this problem as follows.

XAML:

<Window.Resources>
    <DataTemplate x:Key="rbMacros">
        <RadioButton 
            Content="{Binding macro}"
            GroupName="{Binding groupName}"/>
    </DataTemplate>
</Window.Resources>

Code:

    public void PovoarMacros(TabControl tcMacro, DataTemplate template)
    {
        for (int i = 1; i < 11; i++)
        {
            var tab = new TabItem();
            var dg = new DataGrid();
            var nomeMacroColumn = new DataGridTemplateColumn();
            var descricaoMacroColumn = new DataGridTextColumn();

            nomeMacroColumn.Header = "Macro";
            nomeMacroColumn.Width = 425;
            nomeMacroColumn.CanUserResize = false;
            nomeMacroColumn.CanUserReorder = false;
            nomeMacroColumn.CellTemplate = template;
            nomeMacroColumn.IsReadOnly = true;

            descricaoMacroColumn.Header = "Descrição";
            descricaoMacroColumn.Width = 425;
            descricaoMacroColumn.CanUserResize = false;
            descricaoMacroColumn.CanUserReorder = false;
            descricaoMacroColumn.Binding = new Binding("descricao");
            descricaoMacroColumn.IsReadOnly = true;

            dg.Columns.Add(nomeMacroColumn);
            dg.Columns.Add(descricaoMacroColumn);

            for (int j = 0; j < vetor.getVetor(i).Length; j++)
            {
                var macro = (vetor.getVetor(i))[j];
                dg.Items.Add(new Linha() { macro = macro[0].ToString(), descricao = macro[1].ToString(), groupName = vetor.getNomeVetor(i) });
            }

            tab.Header = vetor.getNomeVetor(i);
            tab.Content = dg;
            tcMacro.Items.Add(tab);
        }
    }

Any questions or suggestions are welcome!

    
03.02.2016 / 20:30