0
votes

I am developing UWP app, and I am struggling with treeview checkboxes. I need to know which nodes have checkboxes checked.

I have tried using

mytreeview.SelectedNodes 

but it always returns null. This is my treeview I have also tried using

<TreeViewItem IsSelected="{x:Bind IsSelected ,Mode=TwoWay}" ...>

and in page.cs

public bool IsSelected { get; set; } = false;

but when I check the checkbox it is still false

1

1 Answers

0
votes

Get selected nodes from treeview UWP

Please check this case replay, TreeViewItem contains IsSelected property, we could create model class with IsSelected property and bind it. After item selected IsSelected value will be changed, so you could foreach the itemsource then get the selected item.

public class ExplorerItem : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    public enum ExplorerItemType { Folder, File };
    public String Name { get; set; }
    public ExplorerItemType Type { get; set; }
    private ObservableCollection<ExplorerItem> m_children;
    public ObservableCollection<ExplorerItem> Children
    {
        get
        {
            if (m_children == null)
            {
                m_children = new ObservableCollection<ExplorerItem>();
            }
            return m_children;
        }
        set
        {
            m_children = value;
        }
    }

    private bool m_isExpanded;
    public bool IsExpanded
    {
        get { return m_isExpanded; }
        set
        {
            if (m_isExpanded != value)
            {
                m_isExpanded = value;
                NotifyPropertyChanged("IsExpanded");
            }
        }
    }

    private bool m_isSelected;
    public bool IsSelected
    {
        get { return m_isSelected; }

        set
        {
            if (m_isSelected != value)
            {
                m_isSelected = value;
                NotifyPropertyChanged("IsSelected");
            }
        }

    }

    private void NotifyPropertyChanged(String propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}