0
votes

Say I have a TreeView binding a item source (observable collection), and I have something like this: http://docs.telerik.com/devtools/wpf/controls/radtreeview/how-to/howto-tri-state-mvvm

and each items including parent view has a check box and name(string).

I would like to make the child nodes available only if parent is checked.

And if parent is unchecked, all child node will be unchecked as well.

is there a way I could achieve this? thanks!

1
It is not supported by control itself, so you need to write logic in code.Spawn

1 Answers

2
votes

Couldn't check the link, however, this is a working example and I think you should follow this structure:

View

<Grid>
    <TreeView DataContext="{Binding}" ItemsSource="{Binding Models}">
        <TreeView.ItemTemplate>
            <HierarchicalDataTemplate DataType="{x:Type local:ModelView}" ItemsSource="{Binding Models}">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Name}" />
                    <CheckBox IsChecked="{Binding IsChecked}"/>
                </StackPanel>
            </HierarchicalDataTemplate>
        </TreeView.ItemTemplate>
    </TreeView>
</Grid> 

Model

public class Model : INotifyPropertyChanged
{
    bool _isChecked;
    public bool IsChecked { get { return _isChecked; } set {
            _isChecked = value;
            RaisePropertyChanged("IsChecked");
            if (_isChecked == false)
                unCheckOthers();
        } }
    void unCheckOthers()
    {
        if (_models != null)
            foreach (var m in _models)
                m.IsChecked = false;
    }

    Model[] _models;
    public Model[] Models { get { return _models; } set { _models = value; RaisePropertyChanged("Models"); } }

    string _name;
    public string Name { get { return _name; } set { _name = value; RaisePropertyChanged("Name"); } }

    public event PropertyChangedEventHandler PropertyChanged;
    void RaisePropertyChanged(string propname)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propname));
    }
}

View Model

public class ViewModel
{
    public ViewModel()
    {
        _models = new ObservableCollection<Model>();
    }
    ObservableCollection<Model> _models;
    public ObservableCollection<Model> Models { get { return _models; } set { _models = value; } }
}

Main Window

    public MainWindow()
    {
        InitializeComponent();

        ViewModel mv = new ViewModel();
        Model m1 = new Model() { Name = "model1", IsChecked = true, Desc = "None1" };
        Model m2 = new Model() { Name = "model2", IsChecked = true, Desc = "None2" };
        Model m3 = new Model() { Name = "model3", IsChecked = true, Desc = "None3" };
        Model m4 = new Model() { Name = "model4", IsChecked = true, Desc = "None4", Models = new Model[2] {m1,m2} };
        Model m5 = new Model() { Name = "model5", IsChecked = true, Desc = "None5", Models = new Model[2] { m4, m3 } };
        mv.Models = new ObservableCollection<Model>() { m5 };
        this.DataContext = mv;
    }