Get and Set - TreeView

Viewed 41

I have a TreeView, and I get the selected data this way:

string year = "";
foreach (var a in tbYear.SelectedNodes)
{
     year = year + a.ToString();
}

How can I do the opposite, that is, receive the years and put in TreeView the years I get selected.

Something, like this:

public myclass myinfodata;


<muxc:TreeView x:Name="tbYear"  SelectedNode="{x:Bind myinfodata.year}" SelectionMode="Multiple">
            <muxc:TreeView.RootNodes>
                <muxc:TreeViewNode Content="Year" IsExpanded="False">
                    <muxc:TreeViewNode.Children>
                        <muxc:TreeViewNode Content="1º"/>
                        <muxc:TreeViewNode Content="2º"/>
                        <muxc:TreeViewNode Content="3º"/>
                        <muxc:TreeViewNode Content="4º"/>
                        <muxc:TreeViewNode Content="5º"/>
                        <muxc:TreeViewNode Content="6º"/>
                        <muxc:TreeViewNode Content="7º"/>
                        <muxc:TreeViewNode Content="8º"/>
                        <muxc:TreeViewNode Content="9º"/>
                        <muxc:TreeViewNode Content="10º"/>
                        <muxc:TreeViewNode Content="11º"/>
                        <muxc:TreeViewNode Content="12º"/>
                    </muxc:TreeViewNode.Children>
                </muxc:TreeViewNode>
            </muxc:TreeView.RootNodes>
        </muxc:TreeView>

I tried this way, but it doesn't work

1 Answers

How can I do the opposite, that is, receive the years and put in TreeView the years I get selected.

You should bind your datasource with a treeview DataTemplate, and render each TreeViewItem base on the data structure.

<muxc:TreeView ItemsSource="{x:Bind DataSource}" SelectionMode="Multiple">
    <muxc:TreeView.ItemTemplate>
        <DataTemplate x:DataType="local:ExplorerItem">
            <muxc:TreeViewItem
                Content="{x:Bind Name}"
                IsSelected="{x:Bind IsSelected, Mode=OneWay}"
                ItemsSource="{x:Bind Children}" />
        </DataTemplate>
    </muxc:TreeView.ItemTemplate>
</muxc:TreeView>

You could check above code TreeViewItem has IsSelected property, we can make a matched bool property fo model class and implement INotifyPropertyChanged interface. If we set it as true, the treeview will select current node.

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