2017-03-14 25 views
-1

と、ツリービューでサブアイテムを削除し、私は以下に示すように、私の観察可能なコレクションにバインドされている。このマルチツリービューでサブアイテムを削除しようとしています、私のXAMLの下は、階層的データテンプレート

<loc:MultiSelectTreeView Height="295" ScrollViewer.VerticalScrollBarVisibility="Visible" BorderThickness="1" Background="WhiteSmoke" x:Name="GridListEmulation" Grid.Row="2" BorderBrush="Gray" ItemsSource="{Binding EmulationCollection,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" Margin="0,2,0,-2" 
             ItemContainerStyle="{StaticResource MultiSelectTreeViewItemStyle}" SelectedItemChanged="GridListEmulation_SelectedItemChanged"> 
         <TreeView.ItemTemplate > 
          <HierarchicalDataTemplate ItemsSource="{Binding Items,UpdateSourceTrigger=PropertyChanged,Mode=TwoWay}" > 
           <Grid > 
            <Grid.ColumnDefinitions> 
             <ColumnDefinition SharedSizeGroup="Stream" Width="60"/> 
             <ColumnDefinition SharedSizeGroup="Port" Width="55"/> 
             <ColumnDefinition SharedSizeGroup="Device Name" Width="100"/> 
             <ColumnDefinition SharedSizeGroup="Count" Width="50"/> 
             <ColumnDefinition SharedSizeGroup="FromMAC" Width="120"/> 

             <ColumnDefinition SharedSizeGroup="State" Width="60"/> 
             <ColumnDefinition SharedSizeGroup="MACAddress" Width="120"/> 
             <ColumnDefinition SharedSizeGroup="EmulationIPv4Address" Width="100"/> 
            </Grid.ColumnDefinitions> 
            <TextBlock Grid.Column="0" Text="{Binding StreamId}" Style="{StaticResource TextBlockStyle}"/> 
            <TextBlock Grid.Column="1" Text="{Binding Port}" Style="{StaticResource TextBlockStyle}"/> 
            <TextBlock Grid.Column="2" Text="{Binding EmulationDeviceName}" Style="{StaticResource TextBlockStyle}"/> 
            <TextBlock Grid.Column="3" Text="{Binding SessionCount}" Style="{StaticResource TextBlockStyle}"/> 
            <TextBlock Grid.Column="4" Text="{Binding SourceMAC}" Style="{StaticResource TextBlockStyle}"/> 
            <TextBlock Grid.Column="5" Text="{Binding State}" Style="{StaticResource TextBlockStyle}"/> 
            <TextBlock Grid.Column="6" Text="{Binding SimulatedMAC}" Style="{StaticResource TextBlockStyle}"/> 
            <TextBlock Grid.Column="7" Text="{Binding EmulationIPv4Address}" Style="{StaticResource TextBlockStyle}"/> 
           </Grid> 
          </HierarchicalDataTemplate> 
         </TreeView.ItemTemplate> 
        </loc:MultiSelectTreeView> 

を見つける

ください

tvm.EmulationCollection.RemoveAt(GridListEmulation.Items.IndexOf(subItem));

しかし、私は常にサブアイテムの-1としてインデックスを取得し、例外を与えます。 heirarchicalデータテンプレートのツリービュー項目でサブアイテムを取得して削除する方法があれば教えてください。前もって感謝します。あなたは子供が木から自分自身を削除する責任がある見ることができるように、このXAML

<StackPanel> 
    <Button Content="Add" Command="{Binding AddChild}"/> 
    <TreeView ItemsSource="{Binding Items}"> 
     <TreeView.ItemTemplate> 
      <HierarchicalDataTemplate ItemsSource="{Binding Items}"> 
       <StackPanel Orientation="Horizontal"> 
        <TextBox Text="{Binding Text}"/> 
        <Button Content="Add" Command="{Binding AddChild}"/> 
        <Button Content="Delete" Command="{Binding RemoveMe}"/> 
       </StackPanel> 
      </HierarchicalDataTemplate> 
     </TreeView.ItemTemplate> 
    </TreeView> 
</StackPanel> 

上でホストされ、ここで

+0

ビューモデルの場合、サブアイテムはコレクション内の親アイテムのコレクション内にありませんより良いオプションでは、親の可視性を強制して、子供にそれ自身を削除するように指示することです – MikeT

+0

ありがとうマイク、私たちはどのようにそのサブアイテムの親を取得するのが問題です。 subiteの親を取得する方法はありますか?どんな擬似コードも役に立つでしょう。 – santhosh

+0

親プロパティを親に設定したサブアイテムを作成すると、ツリー構造をトレースする必要がなくなります – MikeT

答えて

0

が、その後

public class TreeVM : BindableBase 
{ 
    public TreeVM() 
    { 
     AddChild = new DelegateCommand(() => Items.Add(new TreeVM() {Parent = this })); 
     RemoveMe = new DelegateCommand(() => Parent.Items.Remove(this)); 
    } 
    private string _Text; 

    public string Text 
    { 
     get { return _Text; } 
     set { SetProperty(ref _Text, value); } 
    } 

    private TreeVM _Parent; 

    public TreeVM Parent 
    { 
     get { return _Parent; } 
     set { SetProperty(ref _Parent, value); } 
    } 

    public ObservableCollection<TreeVM> Items { get; } = new ObservableCollection<TreeVM>(); 

    public DelegateCommand AddChild { get; set; } 
    public DelegateCommand RemoveMe { get; set; } 

} 

ツリーをホストするVMを使用しての基本的な例です。あなたのVMがその親とその子を知っているので、これは機能します