C# WPF-堆叠面板中多个扩展器的问题
我的扩展器有些问题。我还是WPF的新手,也许我遗漏了一些重要的东西,但我不知道是什么。C# WPF-堆叠面板中多个扩展器的问题,c#,wpf,stackpanel,expander,C#,Wpf,Stackpanel,Expander,我的扩展器有些问题。我还是WPF的新手,也许我遗漏了一些重要的东西,但我不知道是什么。 所以我想做的是以编程方式将扩展器添加到Stackpanel,然后将其添加到Scrollviewer,然后将其添加到另一个Stackpanel。 扩展器有4个文本块,每个内容都是动态的。 一切正常,只是我单击的第一个扩展器显示数据。当我展开另一个扩展器时,内容与第一个展开的内容相同。我也只能在折叠先前展开的扩展器时看到展开的扩展器中的数据 这是我的密码: public void CreateDocuments(
所以我想做的是以编程方式将扩展器添加到Stackpanel,然后将其添加到Scrollviewer,然后将其添加到另一个Stackpanel。 扩展器有4个文本块,每个内容都是动态的。
一切正常,只是我单击的第一个扩展器显示数据。当我展开另一个扩展器时,内容与第一个展开的内容相同。我也只能在折叠先前展开的扩展器时看到展开的扩展器中的数据 这是我的密码:
public void CreateDocuments(IcddContainerParser container)
{
DirectoryInfo docDir = new DirectoryInfo(container.GetDocumentFolder());
ScrollViewer scrollViewer = new ScrollViewer();
TextBlock title = new TextBlock();
TextBlock lastEdited = new TextBlock();
TextBlock extension = new TextBlock();
TextBlock size = new TextBlock();
StackPanel textBlockPanel = new StackPanel();
StackPanel expanderPanel = new StackPanel();
foreach (FileInfo file in docDir.GetFiles())
{
Expander expander = new Expander();
textBlockPanel.Children.Clear();
expander.Content = null;
expander.Header = file.Name.ToString();
title.Text = "File Location: " + file.FullName;
textBlockPanel.Children.Add(title);
lastEdited.Text = "Last edited: " + file.LastWriteTime;
textBlockPanel.Children.Add(lastEdited);
extension.Text = "File Type: " + file.Extension;
size.Text = "File Size: " + file.Length.ToString() + " bytes";
textBlockPanel.Children.Add(size);
expander.Content = textBlockPanel;
expander.Name = System.IO.Path.GetFileNameWithoutExtension(file.Name);
expanderPanel.Children.Add(expander);
}
scrollViewer.Content = expanderPanel;
DataPanel.Children.Add(scrollViewer);
return;
}
我的问题:
- 扩展多个扩展器时,扩展器的行为异常(见上文)
- 扩展器文本块仅显示第一个扩展扩展器中的内容
NullReferenceExceptions
。
我还尝试只使用一次该函数,然后在循环中调用该函数,并添加了一个FileInfo文件作为参数,但这只会使情况变得更糟。Replace
textBlockPanel.Children.Clear();
与
替换
textBlockPanel.Children.Clear();
与
好吧这样做了,并将对象创建从循环中的TextBlocks中移动,这就成功了。非常感谢!好吧这样做了,并将对象创建从循环中的TextBlocks中移动,这就成功了。非常感谢!