简体   繁体   中英

TreeView with check boxes and radio buttons

I'd like to create radiobuttons as children of a treeviewitem, but when I do so I can select more than 1 radiobutton. What's more when I select 1 radiobutton i cannot deselect it.

TreeView:

<TreeView Name="tree" Margin="5" Background="LightBlue" ></TreeView>

MainWindow:

public partial class MainWindow : Window
{
    private TreeViewItem createCheckBoxInTree(string content, TreeView tree)
    {
        TreeViewItem item = new TreeViewItem()
        {
            Header = new CheckBox()
            {
                Content = content
            }
        };
        tree.Items.Add(item);
        return item;
    }

    private void createRadioButtonsChildren(string content, TreeViewItem item)
    {
        TreeViewItem childRadio = new TreeViewItem()
        {
            Header = new RadioButton()
            {
                Content = content
            }
        };
        item.Items.Add(childRadio);
    }

    public MainWindow()
    {
        InitializeComponent();
        TreeViewItem parent = createCheckBoxInTree("parent", tree);
        createRadioButtonsChildren("child1", parent);
        createRadioButtonsChildren("child2", parent);
        createRadioButtonsChildren("child3", parent);
    }
}

The reason that the RadioButtons are being selected without resetting the state of the others is that you forgot to set a group for the RadioButtons you create.

Remember that RadioButtons have to belong in a group with at least one being selected. This is the reason why you cannot deselect the. Change the code in your cs file to the one below and it will work as you want.

    protected string RadioButtonGroupName { get; set; }

    private TreeViewItem createCheckBoxInTree(string content, TreeView tree)
    {
        TreeViewItem item = new TreeViewItem()
        {
            Header = new CheckBox()
            {
                Content = content
            }
        };
        tree.Items.Add(item);
        return item;
    }

    private void createRadioButtonsChildren(string content, TreeViewItem item)
    {
        TreeViewItem childRadio = new TreeViewItem()
        {
            Header = new RadioButton()
            {
                Content = content,
                GroupName = RadioButtonGroupName,
            }
        };
        item.Items.Add(childRadio);
    }

    public MainWindow()
    {
        InitializeComponent();

        RadioButtonGroupName = "MyFirstGroup";

        TreeViewItem parent = createCheckBoxInTree("parent", tree);
        createRadioButtonsChildren("child1", parent);
        createRadioButtonsChildren("child2", parent);
        createRadioButtonsChildren("child3", parent);
    }

Please remember to mark this as accepted answer if this solves your problem.

You can give group name for each radio button.

 private void createRadioButtonsChildren(string content, TreeViewItem item)
    {
        TreeViewItem childRadio = new TreeViewItem()
        {
            Header = new RadioButton()
            {
                GroupName="Group1",
                Content = content
            }
        };
        item.Items.Add(childRadio);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM