使用层次数据模板删除树视图中的子项

时间:2017-03-14 10:12:57

标签: wpf mvvm treeviewitem

请在下面找到我的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,然后给出异常。如果有任何方法在子视图数据模板的树视图项中获取子项并删除它,请告诉我?提前谢谢。

1 个答案:

答案 0 :(得分:0)

这是使用VM托管树的基本示例

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

}

然后托管在这个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>

你可以看到孩子负责将自己从树中删除,这是有效的,因为你的VM知道它的父亲及其子女