TreeView的子节点无法反映绑定的变化

时间:2014-10-01 15:12:29

标签: c# wpf mvvm binding treeview

只有顶级节点可以反映后面的变化,但是他们的节点不能。有任何想法吗?谢谢!在以下代码中,它应该同时添加新经理和新员工

模型

public class Employee
{
    public Guid EmployeeId { get; set; }
    public string EmployeeName { get; set; }
}

public class Manager
{
    public Guid ManagerId { get; set; }
    public string ManagerName { get; set; }
    public IEnumerable<Employee> Employees { get; set; }
}

的ViewModels

public class TreeWindowViewModel : INotifyPropertyChanged
{
    private List<Employee> employeeList = new List<Employee>();
    private List<Manager> managerList = new List<Manager>();

    public TreeWindowViewModel()
    {
        //Create fake data for testing
        employeeList.Add(new Employee() { EmployeeId = Guid.NewGuid(), EmployeeName = "Adam" });
        employeeList.Add(new Employee() { EmployeeId = Guid.NewGuid(), EmployeeName = "Benson" });
        employeeList.Add(new Employee() { EmployeeId = Guid.NewGuid(), EmployeeName = "Cooker" });
        employeeList.Add(new Employee() { EmployeeId = Guid.NewGuid(), EmployeeName = "Denny" });
        employeeList.Add(new Employee() { EmployeeId = Guid.NewGuid(), EmployeeName = "Ellis" });

        //Create ObservableCollection object
        employees = new ObservableCollection<Employee>(employeeList);

        //Create 2 managers and make 
        Manager manager1 = new Manager()
        {
            ManagerId = Guid.NewGuid(),
            ManagerName = "Frank",
            Employees = new List<Employee>(employeeList)
        };

        Manager manager2 = new Manager()
        {
            ManagerId = Guid.NewGuid(),
            ManagerName = "Green",
            Employees = new List<Employee>(employeeList)
        };

        managerList.Add(manager1);
        managerList.Add(manager2);

        managers = new ObservableCollection<Manager>(managerList);

    }

    private ObservableCollection<Manager> managers;
    public ObservableCollection<Manager> Managers
    {
        get
        {
            return managers;
        }
        set
        {
            managers = value;

            OnPropertyChanged(new PropertyChangedEventArgs("Managers"));
        }
    }

    private ObservableCollection<Employee> employees;
    public ObservableCollection<Employee> Employees
    {
        get
        {
            return employees;
        }
        set
        {
            employees = value;

            OnPropertyChanged(new PropertyChangedEventArgs("Employees"));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

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

XAML

    <Window x:Class="BindingTests.TreeWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:m="clr-namespace:BindingTests.Models"
        Title="TreeWindow" Height="300" Width="300">
    <Window.Resources>
        <HierarchicalDataTemplate ItemsSource="{Binding Path=Employees}" DataType="{x:Type m:Manager}">
            <TextBlock Text="{Binding Path=ManagerName}"/>
        </HierarchicalDataTemplate>
        <HierarchicalDataTemplate DataType="{x:Type m:Employee}">
            <TextBlock Text="{Binding Path=EmployeeName}"/>
        </HierarchicalDataTemplate>
    </Window.Resources>
    <Grid>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <TreeView ItemsSource="{Binding Managers, Mode=OneWay, NotifyOnSourceUpdated=True}" Grid.Column="0">
            <TreeView.Resources>
                <Style TargetType="Label">
                    <Setter Property="Margin" Value="0"/>
                    <Setter Property="Padding" Value="0"/>
                </Style>
                <Style TargetType="TreeViewItem">
                    <Setter Property="Margin" Value="0"/>
                    <Setter Property="Padding" Value="0"/>
                </Style>
            </TreeView.Resources>
        </TreeView>
        <StackPanel Grid.Column="1">
            <Button Content="Click me to add a manager" Click="Button_Click" Height="30"/>
        </StackPanel>
    </Grid>
</Window>

XAML.CS

    namespace BindingTests
    {
    /// <summary>
    /// Interaction logic for TreeWindow.xaml
    /// </summary>
    public partial class TreeWindow : Window
    {
        public TreeWindow()
        {
            InitializeComponent();

            this.DataContext = new TreeWindowViewModel();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            TreeWindowViewModel ds = this.DataContext as TreeWindowViewModel;
            if (ds != null)
            {
                ds.Managers.Add(new Manager() { ManagerId = Guid.NewGuid(), ManagerName = "New Manager" });
                ds.Employees.Add(new Employee() { EmployeeId = Guid.NewGuid(), EmployeeName = "New Employee"});
            }
        }
    }
}

Initial UI

enter image description here

1 个答案:

答案 0 :(得分:1)

您未获得预期结果的主要原因是您正在将Employee添加到public ObservableCollection<Employee> Employees,并且UI上的任何位置都没有。您真正看到Managers的初始列表与TreeView绑定的内容。 <HierarchicalDataTemplate ItemsSource="{Binding Path=Employees}" ...实际上正在查看Manager.Employees,因此,如果您执行了以下操作,请点击按钮:

if (ds != null)
{
    ds.Managers.Add(new Manager() { ManagerId = Guid.NewGuid(), ManagerName = "New Manager" });
    ds.Managers[0].Employees.Add(new Employee() { EmployeeId = Guid.NewGuid(), EmployeeName = "New Employee"});
}
然后你会看到&#34;新员工&#34;经理和#34;弗兰克。&#34;所以我认为你的代码是可以的,它只是你添加它导致问题的方式。

您的下一个问题可能是,&#34;那我该如何知道将员工添加到谁?&#34;那就是你要在public object SelectedItem中创建TreeWindowViewModel并将其绑定到可以添加到TreeView的依赖项属性的地方,因为TreeView& #39; s SelectedItem是只读的非DP。然后你就可以打电话给:

if(ds.SelectedItem is Manager)
{
    (ds.SelectedItem as Manager).Employees.Add( ... );
}

但是现在我们已经超出了范围,可能会在将来为你提出一个新的SO问题。