我有一个WPF应用程序,允许用户在treeview
中动态创建子项。这些子项是从单独的窗口创建的,并添加到主窗口中。我还想实现一个删除方法,该方法将从单独的窗口中删除主窗口子项。
这些是我的想法以及一些代码:
//Okay button -- Delete sub-items in main window TreeView
private void button2_Click(object sender, RoutedEventArgs e)
{
//Query for Window1
var mainWindow = Application.Current.Windows
.Cast<Window1>()
.FirstOrDefault(window => window is Window1) as Window1;
//Name of header that needs to be located
string header = textBox1.Text;
//While treeview from main window contains subitems
while (!mainWindow.treeView.Items.IsEmpty)
{
//Find TreeView subitem with matching header
//? - not sure on code
//Delete TreeView subitem
//I'm guessing it has something to do with
//mainWindow.treeView.Items.Remove(At?)....
}
}
我的评论显示了我不确定的内容。我已经正确查询了我的mainWindow,并为我想要查找的标题设置了一个字符串值。我已经设置了一个循环来搜索我的treeview
,但是不知道完成工作的确切代码。请告诉我我应该使用的代码。
根据回答修改的代码
我正在尽力理解你的答案。我已经在我的评论中解释了我想要做的事情。我想我正在走上正轨,但在尝试使用RemoveAll
时遇到一点编译器错误。我是否需要添加某种using resourceDictionary
?
代码修订
非常感谢你坚持我。编译器仍然为RemoveAll
的调用提供错误。
//Okay button -- Delete location and corrusponding block
private void button2_Click(object sender, RoutedEventArgs e)
{
//Close Delete Window
this.Close();
//Query for Window1
var mainWindow = Application.Current.Windows
.Cast<Window1>()
.FirstOrDefault(window => window is Window1) as Window1;
//Name of header that needs to be located
string header = textBox1.Text;
//Treeview under operation from main window
TreeViewItem items = mainWindow.treeViewItem;
//Delete corresponding node
RemoveAll(items, p => string.Equals(p.Header, header));
}
//REMOVE ALL METHOD - for use with button_click ^
public void RemoveAll(ItemCollection items, Predicate<TreeViewItem> isValid)
{
for (int i = items.Count - 1; i >= 0; i--)
{
TreeViewItem vItem = (TreeViewItem)items[i];
if (isValid(vItem))
{
items.RemoveAt(i);
}
else
{
RemoveAll(vItem.Items, isValid);
}
}
}
Buld错误
错误1:'...(项目)...'的最佳重载方法匹配有一些无效的参数。
错误2:参数'1':无法从System.Windows.Controls.TreeViewitem
转换为`System.Windows.Controls.ItemCollection'。
谢谢。
答案 0 :(得分:1)
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click_1(object sender, RoutedEventArgs e)
{
string header = txtRemove.Text; //name of Treeviewitem to delete
TreeView view = trvView; //get from location
RemoveAll(view.Items, p => string.Equals(p.Header, header));
}
public bool Remove(ItemCollection items, Predicate<TreeViewItem> isValid)
{
for (int i = items.Count - 1; i >= 0; i--)
{
TreeViewItem vItem = (TreeViewItem)items[i];
if (isValid(vItem))
{
items.RemoveAt(i);
return true;
}
else
{
bool isDeleted = Remove(vItem.Items, isValid);
if (isDeleted)
return isDeleted;
}
}
return false;
}
public void RemoveAll(ItemCollection items, Predicate<TreeViewItem> isValid)
{
for (int i = items.Count - 1; i >= 0; i--)
{
TreeViewItem vItem = (TreeViewItem)items[i];
if (isValid(vItem))
{
items.RemoveAt(i);
}
else
{
RemoveAll(vItem.Items, isValid);
}
}
}
}
///的Xaml
<Window x:Class="TreeViewSample.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>
<TreeView x:Name="trvView" HorizontalAlignment="Left" Height="215" Margin="68,35,0,0" VerticalAlignment="Top" Width="221">
<TreeView.Items>
<TreeViewItem Header="1">
<TreeViewItem Header="1/1"/>
<TreeViewItem Header="1/2"/>
<TreeViewItem Header="1/3"/>
</TreeViewItem>
<TreeViewItem Header="2">
<TreeViewItem Header="2/1"/>
<TreeViewItem Header="2/2"/>
<TreeViewItem Header="2/3"/>
</TreeViewItem>
<TreeViewItem Header="3">
<TreeViewItem Header="3/1"/>
<TreeViewItem Header="3/2"/>
<TreeViewItem Header="3/3"/>
</TreeViewItem>
</TreeView.Items>
</TreeView>
<Button Content="Button" HorizontalAlignment="Left" Margin="333,155,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click_1"/>
<TextBox x:Name="txtRemove" HorizontalAlignment="Left" Height="23" Margin="333,102,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>
</Grid>
</Window>