4

私はこの記事を参照します:

WPF TreeView HierarchicalDataTemplate - 複数の子コレクションを持つオブジェクトへのバインド

次のようにツリー構造を変更します。

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);
    }

特定のノード「エントリ」を右クリックしてエントリの下に新しいノード「ソース」を追加すると (「追加」メソッドを呼び出す)、エントリの下に新しい「ソース」オブジェクトが正常に追加されますが、この新しいノードが表示されません。ツリービューで。ノードの追加/削除時にツリービューを更新する方法は?

4

3 に答える 3

9

コレクション内の何かが変更されたことをユーザー インターフェイスに通知する場合は、IList の代わりにObservableCollectionを使用します。

于 2011-02-10T05:20:43.680 に答える
0

私に関する限り、タイプをItemstoに変更し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したことで状況が変わりました。ツリー構造を構築するメソッドは、実際の型をItemsnew として定義しますList<T>()が、機能し、GUI を更新します。それにもかかわらず、私のツリーは分離コードを使用せずに純粋な MVVM パターンで構築されました。私が使う

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

そして私が使用するビューモデルでは:

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

つまり、アイテムを追加/削除したのではなく、ノードのサブ コレクションを再構築したということです。

于 2016-07-13T11:43:36.430 に答える
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 }
          };
       } 
    }

 }
于 2019-04-30T07:17:52.537 に答える