如何在 WPF 中向扩展器动态添加多个内容
How to dynamically add multiple Contents to an expander in WPF
我有一个方法可以从组合框的输入中在 wpf 中添加多个扩展器。选择组合框项目后,OpenFileDialog 打开,并获取文件名。这种情况不止一次发生,而且我似乎正在为扩展器覆盖我的内容。下面的代码
private void comboBox_SetFileNames(object sender, SelectionChangedEventArgs e)
{
var selectedItem = combobox.SelectedItem as ComboBoxItem;
if (selectedItem != null)
string name = selectedItem.Name;
Expander expander = new Expander {Header = name};
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.DefaultExt = ".txt";
dlg.Filter = "Text File (*.txt) | *.txt";
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
expander.Content = new TextBlock() { Text = System.IO.Path.GetFileName(dlg.FileName) };
}
dlg.Filter = "Excel Files (*.xlsx) | *.xlsx";
Nullable<bool> result2 = dlg.ShowDialog();
if (result2 == true)
{
expander.Content = new TextBlock() { Text = System.IO.Path.GetFileName(dlg.FileName) };
}
dock.Children.Add(expander);
}
}
有什么方法可以让这些文件名一个一个地列在另一个下面?所以像下面这样
ExpanderName
|
------FileName1.txt
|
------FileName2.xlsx
现在它被覆盖了,看起来像这样:
ExpanderName
|
------FileName2.xlsx
将您的 expander.Content
设置为类似 StackPanel
的面板,然后将您的 TextBlocks
添加到其中。
Content
属性只能设置一个值,而像StackPanel
这样的面板可以包含多个控件。
像这样:
Expander expander = new Expander {Header = name};
StackPanel panel = new StackPanel();
var dlg = new Microsoft.Win32.OpenFileDialog();
dlg.DefaultExt = ".txt";
dlg.Filter = "Text File (*.txt) | *.txt";
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
panel.Children.Add(new TextBlock() { Text = dlg.SafeFileName });
dlg.Filter = "Excel Files (*.xlsx) | *.xlsx";
Nullable<bool> result2 = dlg.ShowDialog();
if (result2 == true)
panel.Children.Add(new TextBlock() { Text = dlg.SafeFileName });
expander.Content = panel;
dock.Children.Add(expander);
我有一个方法可以从组合框的输入中在 wpf 中添加多个扩展器。选择组合框项目后,OpenFileDialog 打开,并获取文件名。这种情况不止一次发生,而且我似乎正在为扩展器覆盖我的内容。下面的代码
private void comboBox_SetFileNames(object sender, SelectionChangedEventArgs e)
{
var selectedItem = combobox.SelectedItem as ComboBoxItem;
if (selectedItem != null)
string name = selectedItem.Name;
Expander expander = new Expander {Header = name};
Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
dlg.DefaultExt = ".txt";
dlg.Filter = "Text File (*.txt) | *.txt";
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
{
expander.Content = new TextBlock() { Text = System.IO.Path.GetFileName(dlg.FileName) };
}
dlg.Filter = "Excel Files (*.xlsx) | *.xlsx";
Nullable<bool> result2 = dlg.ShowDialog();
if (result2 == true)
{
expander.Content = new TextBlock() { Text = System.IO.Path.GetFileName(dlg.FileName) };
}
dock.Children.Add(expander);
}
}
有什么方法可以让这些文件名一个一个地列在另一个下面?所以像下面这样
ExpanderName
|
------FileName1.txt
|
------FileName2.xlsx
现在它被覆盖了,看起来像这样:
ExpanderName
|
------FileName2.xlsx
将您的 expander.Content
设置为类似 StackPanel
的面板,然后将您的 TextBlocks
添加到其中。
Content
属性只能设置一个值,而像StackPanel
这样的面板可以包含多个控件。
像这样:
Expander expander = new Expander {Header = name};
StackPanel panel = new StackPanel();
var dlg = new Microsoft.Win32.OpenFileDialog();
dlg.DefaultExt = ".txt";
dlg.Filter = "Text File (*.txt) | *.txt";
Nullable<bool> result = dlg.ShowDialog();
if (result == true)
panel.Children.Add(new TextBlock() { Text = dlg.SafeFileName });
dlg.Filter = "Excel Files (*.xlsx) | *.xlsx";
Nullable<bool> result2 = dlg.ShowDialog();
if (result2 == true)
panel.Children.Add(new TextBlock() { Text = dlg.SafeFileName });
expander.Content = panel;
dock.Children.Add(expander);