简体   繁体   English

带有复选框和单选按钮的TreeView

[英]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. 我想创建单选按钮作为treeviewitem的子代,但是当我这样做时,可以选择多个单选按钮。 What's more when I select 1 radiobutton i cannot deselect it. 而且,当我选择1个单选按钮时,我无法取消选择它。

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. 选择RadioButton而不重置其他状态的原因是您忘记为创建的RadioButton设置组。

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. 将您的cs文件中的代码更改为下面的代码,它可以根据需要工作。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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