我正在研究WPF TreeView
。我可以在树下添加新项目,但我无法从列表中删除它们。在我的代码中,我试图获取所选树项的索引并尝试删除它。但代码返回索引“-1”。这会产生ArgumentOutOfRangeException
。
请帮忙解决这个问题。
<Window x:Class="MyTreeStructure.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Content="ADD" Height="23" HorizontalAlignment="Left" Margin="211,50,0,0"
Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
<TreeView Height="200" HorizontalAlignment="Left" Margin="27,12,0,0" Name="treeView1"
VerticalAlignment="Top" Width="120" >
<TreeViewItem Name="Parent" Header="My Tree Content">
</TreeViewItem>
</TreeView>
<TextBox Height="23" HorizontalAlignment="Left" Margin="211,12,0,0" Name="textBox1"
VerticalAlignment="Top" Width="120" />
<Button Content="Delete" Height="23" HorizontalAlignment="Left" Margin="211,79,0,0"
Name="button2" VerticalAlignment="Top" Width="75" Click="button2_Click" />
</Grid>
</Window>
namespace MyTreeStructure
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void button1_Click(object sender, RoutedEventArgs e)
{
TreeViewItem temp = new TreeViewItem();
temp.Header = textBox1.Text;
Parent.Items.Add(temp);
}
private void button2_Click(object sender, RoutedEventArgs e)
{
textBox1.Text = treeView1.SelectedItem.ToString();
Parent.Items.RemoveAt(treeView1.Items.IndexOf(treeView1.SelectedItem));
**// Here I am getting exception. What should be the code here ??**
}
}
}
答案 0 :(得分:1)
有问题
treeView1.Items.IndexOf(treeView1.SelectedItem))
由于treeview1只有一个节点“Parent”,因此您添加的节点的其余部分位于名为“Parent”的节点中。
因此,如果您尝试获取treeView1.Items中节点的索引,它将返回-1,除了名为“Parent”的节点,它将返回0。
所以你修改了删除节点的代码,如下所示。
private void button2_Click(object sender, RoutedEventArgs e)
{
textBox1.Text = treeView1.SelectedItem.ToString();
int index = treeView1.Items.IndexOf(treeView1.SelectedItem));
if(index < 0)
{
index = Parent.Items.IndexOf(treeView1.SelectedItem));
}
if(index > 0)
{
Parent.Items.RemoveAt(index);
}
}
答案 1 :(得分:0)
我不熟悉WPF,但在WinForms中,您的方法会导致错误。第一个可能是项目内部计算的结果。它是这样的: 0 -0 -1 -2 1 -0 -0 -1 -1 ......
另一个绊脚石是,如果找不到该项,IndexOf将返回-1(如您所述)。您必须先检查值是否为-1,然后导航到Treeview.Nodes.Nodes中的正确子列表...最后调用RemoveAt()。
我希望这很有帮助 帕特里克
答案 2 :(得分:0)
为什么不使用Parent.Items.Remove(treeView1.SelectedItem)
?
答案 3 :(得分:0)
这是我的两分钱。这段代码正在我的机器上运行。
TreeViewItem t;
private void button2_Click(object sender, RoutedEventArgs e)
{
// Delete the node
Parent.Items.RemoveAt(Parent.Items.IndexOf(t));
}
private void treeView1_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
// Get the selected node
t = (TreeViewItem)(((TreeView)e.Source).SelectedItem);
}
答案 4 :(得分:-1)
<Grid>
<TextBox Height="23" HorizontalAlignment="Left" Margin="8,14,0,0" Name="textBox1" VerticalAlignment="Top" Width="127" />
<Button Height="23" Margin="140,14,0,0" Name="button1" VerticalAlignment="Top" HorizontalAlignment="Left" Width="76" Click="button1_Click">Add Item</Button>
<Button Height="23" Margin="226,14,124,0" Name="DeleteButton" VerticalAlignment="Top" Click="DeleteButton_Click">Delete Item</Button>
<TreeView Margin="10,100,0,13" Name="TreeView1" HorizontalAlignment="Left" VerticalAlignment="Top" Width="194" Height="200">
<TreeViewItem x:Name="Parent" Header="Cold Drinks">
<TreeViewItem Header="Coke"></TreeViewItem>
<TreeViewItem Header="Pepsi"></TreeViewItem>
<TreeViewItem Header="Orange Juice"></TreeViewItem>
<TreeViewItem Header="Milk"></TreeViewItem>
<TreeViewItem Header="Iced Tea"></TreeViewItem>
<TreeViewItem Header="Mango Shake"></TreeViewItem>
</TreeViewItem>
</TreeView>
</Grid>
private void button1_Click(object sender, RoutedEventArgs e)
{
TreeViewItem newChild = new TreeViewItem();
newChild.Header = textBox1.Text;
Parent.Items.Add(newChild);
}
private void DeleteButton_Click(object sender, RoutedEventArgs e)
{
int index = TreeView1.Items.IndexOf(TreeView1.SelectedItem);
if (index < 0)
{
index = Parent.Items.IndexOf(TreeView1.SelectedItem);
}
if (index >= 0 && ((System.Windows.Controls.TreeViewItem)TreeView1.SelectedItem).Name.Contains("Parent"))
{
TreeView1.Items.RemoveAt(index);
}
else if (index >= 0)
{
Parent.Items.RemoveAt(index);
}
}