WPF TreeView - 如何在添加/删除节点后刷新树?

问题描述 投票:4回答:3

我参考这篇文章:

WPF TreeView HierarchicalDataTemplate - binding to object with multiple child collections

并修改树结构,如:

Root
  |__Group
       |_Entry
           |_Source

在Entry.cs中:

public class Entry
{
    public int Key { get; set; }
    public string Name { get; set; }

    public ObservableCollection<Source> Sources { get; set; }

    public Entry()
    {
        Sources = new ObservableCollection<Source>();
    }

    public ObservableCollection<object> Items
    {
        get
        {
            ObservableCollection<object> childNodes = new ObservableCollection<object>();

            foreach (var source in this.Sources)
                childNodes.Add(source);

            return childNodes;
        }
    }
}

在Source.cs中:

public class Source
{
    public int Key { get; set; }
    public string Name { get; set; }
}

在XAML文件中:

<UserControl.CommandBindings>
    <CommandBinding Command="New" Executed="Add" />
</UserControl.CommandBindings>

    <TreeView x:Name="TreeView">
        <TreeView.ItemContainerStyle>
            <Style TargetType="{x:Type TreeViewItem}">
                <Setter Property="TreeViewItem.IsExpanded" Value="True"/>
            </Style>
        </TreeView.ItemContainerStyle>

        <TreeView.Resources>
            <HierarchicalDataTemplate DataType="{x:Type local:Root}" ItemsSource="{Binding Items}">
                 <TextBlock Text="{Binding Path=Name}" IsEnabled="True">
                 </TextBlock>
            </HierarchicalDataTemplate>

            <HierarchicalDataTemplate DataType="{x:Type local:Group}" ItemsSource="{Binding Items}">
                <TextBlock Text="{Binding Path=Name}" IsEnabled="True">
                </TextBlock>
            </HierarchicalDataTemplate>


            <HierarchicalDataTemplate DataType="{x:Type local:Entry}" ItemsSource="{Binding Items}">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding Path=Name}" IsEnabled="True">
                        <TextBlock.ContextMenu>
                            <ContextMenu >
                                <MenuItem Header="Add" Command="New">
                                </MenuItem>
                            </ContextMenu>
                        </TextBlock.ContextMenu>
                    </TextBlock>
                </StackPanel>
            </HierarchicalDataTemplate>


            <DataTemplate DataType="{x:Type local:Source}" >
                <TextBlock Text="{Binding Path=Name}" />
            </DataTemplate>

        </TreeView.Resources>
    </TreeView>

在UserControl.cs中:

public ObservableCollection<Root> Roots = new ObservableCollection<Root>();

    public UserControl6()
    {
        InitializeComponent();

        //...Add new node manually

        TreeView.ItemsSource = Roots;
    }

    private void Add(object sender, ExecutedRoutedEventArgs e)
    {
        Entry ee = (Entry)TreeView.SelectedItem;
        Source s3 = new Source() { Key = 3, Name = "New Source" };
        ee.Sources.Add(s3);
    }

当我在特定节点“Entry”上单击右键以在Entry(调用“Add”方法)下添加新节点“Source”时,我在Entry下成功添加了一个新的“Source”对象,但是我看不到这个新节点在树视图上。添加/删除节点时如何刷新树视图?

c# wpf xaml treeview hierarchicaldatatemplate
3个回答
6
投票

如果要通知用户界面集合中的某些内容已更改,请使用ObservableCollection而不是IList


0
投票

就我而言,将Items的类型改为ObservableCollection<T>将无法解决问题。你需要实现INotifyPropertyChanged。我为树视图测试了两个解决方案,因为我遇到了同样的问题。在我的情况下,从IList到ObservableCollection的类型更改没有刷新GUI。但是当我改变我的汽车财产时:

public List<SourceControlItemViewBaseModel> Items { get; set; }

 private IEnumerable<SourceControlItemViewBaseModel> _items;
    public IEnumerable<SourceControlItemViewBaseModel> Items
    {
        get { return _items; }
        set
        {
            _items = value;
            OnPropertyChanged();
        }
    }

也就是说,我已经实施了INotifyPropertyChanged,这改变了这种情况。构建树结构的方法将Items的实际类型定义为新的List<T>(),但它可以工作并刷新GUI。然而,我的树是用纯MVVM模式构建的,没有使用代码隐藏。我用

<TreeView ItemsSource="{Binding SourceControlStructureItems}" />

在我使用的视图模型中:

  currentVm.Items= await SourceControlRepository.Instance.BuildSourceControlStructureAsync(currentVm.ServerPath);

这意味着我没有添加/删除项目,但我重建了Node的子集合。


0
投票

使用此类,Sources集合中的任何更改都将在UI中更新/刷新树。

public class Entry
{
    public int Key { get; set; }
    public string Name { get; set; }

    public ObservableCollection<Source> Sources { get; set; }

    public Entry()
    {
        Sources = new ObservableCollection<Source>();
    }

    public CompositeCollection Items
    {
       get
       {
          return new CompositeCollection()
          {
             new CollectionContainer() { Collection = Sources },
             // Add other type of collection in composite collection
             // new CollectionContainer() { Collection = OtherTypeSources }
          };
       } 
    }

 }
© www.soinside.com 2019 - 2024. All rights reserved.