在折叠树视图时,不会保留选择项

时间:2017-02-23 08:54:47

标签: c# wpf mvvm

我有父节点的树视图,其中包含子项。我正在选择子节点并在折叠相应的父节点时,不会保留选择。

XAML:

 <Grid>
    <TreeView Name="trvPersons" SelectedItemChanged="trvPersons_SelectedItemChanged">
         <TreeView.ItemTemplate>
             <HierarchicalDataTemplate ItemsSource="{Binding Children}">
                 <StackPanel Orientation="Horizontal">
                     <TextBlock Text="{Binding Name}" Margin="0,0,4,0" x:Name="txt"/>
                 </StackPanel>
             </HierarchicalDataTemplate>
         </TreeView.ItemTemplate>
         <TreeView.ItemContainerStyle>
             <Style TargetType="TreeViewItem">
                 <Setter Property="IsSelected" Value="{Binding IsChecked}" />
             </Style>
         </TreeView.ItemContainerStyle>
     </TreeView>
 </Grid>

代码背后:

public partial class MainWindow : Window
{
    public static List<Person> persons = new List<Person>();
    public MainWindow()
    {
        InitializeComponent();

        Person person1 = new Person() { Name = "John Doe", uniqueName="1",  Age = 42 };          
        person1.Children.Add(new Person() { Name = "Sammy Doe", uniqueName = "2", parentUniqueName = "1", Age = 13 });
        Person person2 = new Person() { Name = "Jane Doe", uniqueName = "3", Age = 39 };
        person2.Children.Add(new Person() { Name = "Jenny Moe\\",  uniqueName = "4", parentUniqueName = "3", Age = 17 });
        person2.Children.Add(new Person() { Name = "Jenny Moe", uniqueName = "5", parentUniqueName = "3", Age = 17 });
        Person person3 = new Person() { Name = "Becky Toe", uniqueName = "6", Age = 25 };
        person3.Children.Add(new Person() { Name = "Jenny Moe", uniqueName = "7", parentUniqueName = "6", Age = 17 });
        persons.Add(person1);
        persons.Add(person2);
        persons.Add(person3);    

        trvPersons.ItemsSource = persons;
    }

    private void trvPersons_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
    {
    }

    public class Person : INotifyPropertyChanged
    {
        public List<Person> Children { get; set; }
        public string uniqueName { get; set; }
        public string parentUniqueName { get; set; }

        public Person()
        {
            this.Children = new List<Person>();
        }

        public string Name { get; set; }
        public int Age { get; set; }
        private bool isChecked;

        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public bool IsChecked
        {
            get { return this.isChecked; }
            set
            {
                this.isChecked = value;
                if (value && !string.IsNullOrEmpty(this.parentUniqueName))
                {
                    persons.ForEach(i => i.IsChecked = false);
                    var p = MainWindow.persons.FirstOrDefault(i => i.uniqueName == this.parentUniqueName);
                    if (p != null)
                    {
                        p.IsChecked = true;
                    }
                }
                OnPropertyChanged("IsChecked");
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

如果单击父节点进行折叠,基本上在WPF树视图中,所选项目将更改为父节点