【发布时间】:2019-11-17 19:10:02
【问题描述】:
我在使用扩展器时遇到了一些问题。我还是 WPF 的新手,也许我错过了一些重要的东西,但我不知道是什么。
所以我想做的是以编程方式将扩展器添加到 Stackpanel,然后将其添加到 Scrollviewer,然后将其添加到另一个 Stackpanel。
扩展器有 4 个文本块,每个内容都是动态的。
一切正常,除了我点击的第一个扩展器显示数据。当我展开另一个 Expander 时,内容与第一个展开的内容相同。当我折叠先前展开时,我也只能在展开的展开器中看到数据。
这是我的代码:
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 file 作为参数,但这只会让情况变得更糟。
【问题讨论】:
标签: c# wpf stackpanel expander