dimanche 23 septembre 2018

Add a checkboxes to existing Treeview in WPF

I have an existing treeview in WPF in which I would like to add checkboxes

Here the code

I have a class Person which contains all the structure

Person.cs

public class Person
    {
        readonly List<Person> _children = new List<Person>();
        public IList<Person> Children
        {
            get { return _children; }
        }

        public string Name { get; set; }
    }

As I read in some other posts, I use ViewModel

PersonViewModel.cs

public class PersonViewModel : INotifyPropertyChanged
    {
        #region Data
        readonly ReadOnlyCollection<PersonViewModel> _children;
        readonly PersonViewModel _parent;
        readonly Person _person;

        bool _isExpanded=true;
        bool _isSelected;
        #endregion Data

        #region Constructors
        public PersonViewModel(Person person): this(person, null)
        {
        }

        private PersonViewModel(Person person, PersonViewModel parent)
        {
            _person = person;
            _parent = parent;

            _children = new ReadOnlyCollection<PersonViewModel>(
                    (from child in _person.Children
                     select new PersonViewModel(child, this))
                     .ToList<PersonViewModel>());
        }
        #endregion Constructors

        #region Person Properties
        public ReadOnlyCollection<PersonViewModel> Children
        {
            get { return _children; }
        }

        public string Name
        {
            get { return _person.Name; }
        }

        #endregion Person Properties

        #region Presentation Members
        #region IsExpanded

        /// <summary>
        /// Gets/sets whether the TreeViewItem 
        /// associated with this object is expanded.
        /// </summary>
        public bool IsExpanded
        {
            get { return _isExpanded; }
            set
            {
                if (value != _isExpanded)
                {
                    _isExpanded = value;
                    OnPropertyChanged("IsExpanded");
                }

                // Expand all the way up to the root.
                if (_isExpanded && _parent != null)
                    _parent.IsExpanded = true;
            }
        }

        #endregion IsExpanded

        #region IsSelected

        /// <summary>
        /// Gets/sets whether the TreeViewItem 
        /// associated with this object is selected.
        /// </summary>
        public bool IsSelected
        {
            get { return _isSelected; }
            set
            {
                if (value != _isSelected)
                {
                    _isSelected = value;
                    OnPropertyChanged("IsSelected");
                }
            }
        }

        #endregion IsSelected

        #region NameContainsText

        public bool NameContainsText(string text)
        {
            if (String.IsNullOrEmpty(text) || String.IsNullOrEmpty(this.Name))
                return false;

            return Name.IndexOf(text, StringComparison.InvariantCultureIgnoreCase) > -1;
        }

        #endregion NameContainsText

        #region Parent

        public PersonViewModel Parent
        {
            get { return _parent; }
        }

        #endregion Parent

        #endregion Presentation Members        

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion INotifyPropertyChanged Members
    }

The family tree ViewModel

FamilyTreeViewModel.cs

public class FamilyTreeViewModel {

#region Data
readonly PersonViewModel _rootPerson;
#endregion Data

#region Constructor
public FamilyTreeViewModel(Person rootPerson)
{
    _rootPerson = new PersonViewModel(rootPerson);

    FirstGeneration = new ReadOnlyCollection<PersonViewModel>(
        new PersonViewModel[]
        {
            _rootPerson
        });
}
#endregion Constructor

#region Properties
#region FirstGeneration
/// <summary>
/// Returns a read-only collection containing the first person 
/// in the family tree, to which the TreeView can bind.
/// </summary>
public ReadOnlyCollection<PersonViewModel> FirstGeneration { get; }
#endregion FirstGeneration
#endregion Properties

}

The xaml code MainWindow.xaml

<TreeView ItemsSource="{Binding FirstGeneration}">
    <TreeView.ItemContainerStyle>
        <Style TargetType="{x:Type TreeViewItem}">
            <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
            <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
            <Setter Property="FontWeight" Value="Normal" />
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="FontWeight" Value="Bold" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </TreeView.ItemContainerStyle>

    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding Children}">
            <TextBlock Text="{Binding Name}" />

        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

MainWindow.xaml.cs

  public partial class MainWindow : Window
    {
        readonly FamilyTreeViewModel _familyTree;

        public MainWindow()
        {
            InitializeComponent();
            Person rootPerson = new Person
            {
                Name="Application Architect Right",
                Children =
                {
                    new Person
                    {
                        Name="Generate"
                    },
                    new Person
                    {
                        Name="Instances rights",
                        Children =
                        {
                            new Person
                            {
                                Name = "Create"
                            },
                            new Person
                            {
                                Name = "Modify"
                            },
                            new Person
                            {
                                Name = "Delete"
                            },
                            new Person
                            {
                                Name = "Exceptions Management"
                            }
                        }

                    },
                    new Person
                    {
                        Name="Templates rights",
                        Children =
                        {
                            new Person
                            {
                                Name = "Create"
                            },
                            new Person
                            {
                                Name = "Modify"
                            },
                            new Person
                            {
                                Name = "Delete"
                            }
                        }
                    },
                    new Person
                    {
                        Name="Parameters rights",
                        Children =
                        {
                            new Person
                            {
                                Name = "Create"
                            },
                            new Person
                            {
                                Name = "Modify"
                            },
                            new Person
                            {
                                Name = "Delete"
                            }
                        }
                    },
                }
            };


            // Create UI-friendly wrappers around the 
            // raw data objects (i.e. the view-model).
            _familyTree = new FamilyTreeViewModel(rootPerson);


            // Let the UI bind to the view-model.
            DataContext = _familyTree;

        }
    }

Can someone can help me?

Thanks in advance




Aucun commentaire:

Enregistrer un commentaire