Adding Radiobutton to the Datagrid in Runtime

Asked

Viewed 51 times

2

At a certain point in the software, I need to populate a DataGrid in Runtime. I managed to perform this operation partially. My problem is that even if I insert RadioButton.Content, it simply does not present the past Content. Also, the Group does not work in RadioButton.

Follow the code to facilitate understanding.

    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);
        }
    }

1 answer

1


I managed 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 will be welcome!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.