I have a method that adds multiple expanders in wpf, from the input of a combobox. After the combobox item is selected, a OpenFileDialog opens, and gets a filename. This happens more than once, and I seem to be overwriting my Content for the expander. Code below
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);
}
}
Any way to have each of these file names listed below one another? so something like below
ExpanderName
|
------FileName1.txt
|
------FileName2.xlsx
Right now with it getting overwritten it looks like this:
ExpanderName
|
------FileName2.xlsx
expander.Content
to aStackPanel
, and add yourTextBlocks
to theStackPanel
. TheContent
property can only be set to a single value, while a StackPanel can contain multiple controls. – Rachel