Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
HOW DO I ADD THE SAME EXPANDER MULTIPLE TIMES TO A STACK PANEL IN WPF
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using MotionControl.PropertiesForms;


namespace MotionControl
{
///
/// Interaction logic for MainWindow.xaml
///

public partial class MainWindow : Window
{


public MainWindow()
{



InitializeComponent();






DispatcherTimer timer1 = new DispatcherTimer();
timer1.Interval = TimeSpan.FromSeconds(1);
timer1.Tick += timer_Tick;
timer1.Start();
}
void timer_Tick(object sender, EventArgs e)
{



if (MicroPosition.Default.DeviceName == "Micro Position Control")
{

expander2.Visibility = Visibility.Visible;



// create new tab item


// add controls to tab item, this case I added just a textbox



stackpanel1.Children.Add(expander2);


}
else if (MicroPosition.Default.DeviceName == "Temperature Sensor")
{
Display dis = new Display();






expander3.Visibility = Visibility.Visible;
if (MicroPosition.Default.DeviceName == "Temperature Sensor")
{
for (int i = 0; i <= 15; i++)
{
if (i > 0)
{
stack.Children.Add(expander3);
}
}

}
else if (MicroPosition.Default.DeviceName == "New Devices")
{
expander4.Visibility = Visibility.Visible;
stack.Children.Add(expander4);

}




}



}
}
}
Posted
Updated 23-Apr-15 21:08pm
v2
Comments
Sergey Alexandrovich Kryukov 24-Apr-15 3:23am    
Why?
—SA

1 solution

This is a special feature of WPF: no object can be added more than once to the logical tree of the whole UI (see also: https://msdn.microsoft.com/en-us/library/ms753391%28v=vs.110%29.aspx[^]).

On attempt to add a reference to the same UI element in more than two places in the UI, an exception will be thrown. If you want to add several expanders, you have to create several different instances of System.Windows.Controls.Expander.

—SA
 
Share this answer
 
v2
Comments
Member 11521909 24-Apr-15 3:31am    
how do u create those instances? will i be able to create the expanders with the same content in it?
Sergey Alexandrovich Kryukov 24-Apr-15 3:39am    
In a usual way, via the constructor; or XAML will do it for you.
—SA
Member 11521909 24-Apr-15 3:49am    
if it is not much of a trouble for u, could u type down the code
Sergey Alexandrovich Kryukov 24-Apr-15 3:59am    
Sure:
var expander1 = new System.Windows.Controls.Expander();
var expander2 = new System.Windows.Controls.Expander();
//...
somePanel.Children.Add( // for example,
expander1);
somePanel.Children.Add(expander2);
//...

But what's wrong with using XAML? Perhaps, variable number of UI elements changing during runtime?...

—SA
Member 11521909 24-Apr-15 3:33am    
the expander name is micro position controller n it appears on clicking a button n i want another expander by the name micro position controller2 on clicking the same button with the same content in it. is that possible?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900