如何在WPF中将相同的扩展器多个时间添加到堆叠面板 [英] HOW DO I ADD THE SAME EXPANDER MULTIPLE TIMES TO A STACK PANEL IN WPF

查看:147
本文介绍了如何在WPF中将相同的扩展器多个时间添加到堆叠面板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在WPF中将相同的扩展器多个时间添加到堆叠面板

使用System;

使用System.Collections.Generic;

使用System.Linq;

使用System.Text;

使用System.Windows;

使用System.Windows.Controls;

使用System.Windows.Data;

使用System.Windows.Documents;

使用System.Windows.Input;

使用System.Windows.Media;

使用System.Windows.Media.Imaging;

使用System.Windows.Navigation;

使用System .Windows.Shapes;

使用System.Windows.Threading;

使用MotionControl.PropertiesForms;





命名空间MotionControl

{

///< summary>

/// MainWindow.xaml的交互逻辑

///

公共部分类MainWindow:窗口

{





public MainWindow()

{< br $> b $ b





InitializeComponent();



$ /








DispatcherTimer timer1 = new DispatcherTimer();

timer1.Interval = TimeSpan.FromSeconds(1);

timer1.Tick + = timer_Tick;

timer1.Start();

}

void timer_Tick(对象发送者,EventArgs e)

{







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

{



expander2.Visibility = Visibility.Visible;







//创建新标签项





//为标签项添加控件,这个案例我只添加了一个文本框







stackpanel1.Children。添加(扩展器2);





}

else if(MicroPosition.Default.DeviceName ==Temperature传感器)

{

显示dis = new显示();





$ / b $ b $ / b


expander3.Visibility = Visibility.Visible;

if(MicroPosition.Default.DeviceName ==Temperature Sensor)

{

for(int i = 0;我< = 15; i ++)

{

if(i> 0)

{

stack.Children.Add(expander3) );

}

}



}

否则(MicroPosition。 Default.DeviceName ==New Devices)

{

expander4.Visibility = Visibility.Visible;

stack.Children.Add(expander4 );



}









}







}

}

}

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
{
/// <summary>
/// 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);

}




}



}
}
}

推荐答案

这是WPF的一个特殊功能:没有任何对象可以多次添加到逻辑整个UI的树(另请参阅: https://msdn.microsoft.com/en-us/library/ms753391%28v=vs.110%29.aspx [ ^ ])。



尝试在UI中的两个以上位置添加对同一UI元素的引用时,将引发异常。如果你想添加几个扩展器,你必须创建几个不同的 System.Windows.Controls.Expander 实例。



-SA
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


这篇关于如何在WPF中将相同的扩展器多个时间添加到堆叠面板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆