删除WPF中的所有Treeview节点 [英] Remove all treeview nodes in wpf

查看:263
本文介绍了删除WPF中的所有Treeview节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在使用wpf treeview.
我已经用它的数据上下文属性绑定了树视图,但是每当我试图通过清除所有节点来刷新树视图时,都不允许这样做.
我尝试使用treeview1.items.clear();.但这会引发异常.
请建议我如何解决这个问题.

异常详细信息

Hi,

I am working with wpf treeview.
I have bound the treeview with it''s data context property, but whenever I am trying to refresh the treeview by clearing all the nodes, it''s not allowing.
I have tried using treeview1.items.clear();. But it throws exception.
Please suggest me how can I solve this problem.

Exception Details

System.InvalidOperationException was caught
  Message=Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead.
  Source=PresentationFramework
  StackTrace:
       at System.Windows.Controls.ItemCollection.Clear()
       at GDMDomainsStandardization.DataSource.btnBrowse_Click(Object sender, RoutedEventArgs e) in E:\Snehasis\Code\GDMDomainsStandardization\GDMDomainsStandardization\DataSource.xaml.cs:line 78
  InnerException:



代码



Code

private void btnBrowse_Click(object sender, RoutedEventArgs e)
{
try
{
 
// Create OpenFileDialog
Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog();

// Set filter for file extension and default file extension
openFileDialog.DefaultExt = ".xlsx";
openFileDialog.Filter = "Excel documents (.xlsx)|*.xlsx|Excel documents (.xls)|*.xls|All Files (*.*)|*.*";

// Display OpenFileDialog by calling ShowDialog method
Nullable result = openFileDialog.ShowDialog();

// Get the selected file path and display in a TextBox
if (result == true)
{
txtFilePath.Text = openFileDialog.FileName;
if (txtFilePath.Text.Contains(':'))
{
Util util = new Util();
txtFilePath.Text = ("\\\\" + util.GetLocalIP() + "\\" + txtFilePath.Text).Replace(":", ConfigurationManager.AppSettings["replaceChar"].ToString());
}
References.excelFilePath = txtFilePath.Text;
References.ReturnTableDetails.Clear();
//Making textbox readonly
txtFilePath.IsReadOnly = true; 
List GetTreeViewItems = ExcelViewModel.CreateTreeviewStructure(txtFilePath.Text, Common.Config.GetDefaultColumnName());
if (ExcelViewModel.HasRequiredSheets == false)
{
MessageBox.Show("Please select a excel workbook which contains atleast one Commodity1 column in any one of the sheets. ", Common.Config.GetDomainsStandarization(), MessageBoxButton.OK, MessageBoxImage.Information);
//Here I am trying to Clear the treeview itemstvMain.Items.clear();
}
else
{
//Binding excel sheet names and their columns with treeview. 
For the first time when i click browse it's working fine.but second time when i try to modifi treeview it throws the errortvMain.DataContext = GetTreeViewItems;
}

}

}
catch (Exception ex)
{
MessageBox.Show("Exception in browsing the input filename. Please check the following error " + ex.Message, Common.Config.GetAppSetting("DomainsStandardization"), MessageBoxButton.OK, MessageBoxImage.Error);
}
}


问候,
Snehasis


Regards,
Snehasis

推荐答案

使用数据绑定填充树形视图节点时,不支持直接修改节点.异常消息告诉您这种情况...

如果我做对了,您只想在树状视图中不显示任何项目?对于您的情况,将treeviews的DataContext属性设置为null(或空集合)应该可以解决问题.

不过:如果您打算继续使用数据绑定,则应查看 MVVM模式 [
when the treeview-nodes are populated using databinding, direct modifications of the nodes is not supported. the exception message tells you that circumstance...

if i get it right you want to simply show no items in the treeview? for your case, setting the treeviews DataContext property to null (or an empty collection) should do the trick.

Nevertheless: if you plan to continue using databinding, you should take a look at the MVVM-pattern[^] and its (more or less) built-in support in WPF.


这篇关于删除WPF中的所有Treeview节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆