我正在WPF(C#)中创建一个基本应用程序,我想使用 UserControl 。我已经创建了3个示例UserControl和一个页面窗口,其中包含按钮,标签,面板等。我想在单击按钮上加载每个UserControl。你可以在下面看到我的逻辑(代码) -
public partial class Dashboard : Window
{
public Dashboard()
{
InitializeComponent();
}
private void btn1_Click(object sender, RoutedEventArgs e)
{
Dock_MainPanel.Controls.Clear();
Dock_MainPanel.Visible = true;
Sample1 usr1 = new Sample1();
usr1.Show();
Dock_MainPanel.Controls.Add(usr1);
}
private void btn2_Click(object sender, RoutedEventArgs e)
{
//SAMPLE CODE
}
}
现在我的问题是这段代码无效。它说明了一些错误。它在图像中显示如下 -
基本上我想在点击各自的按钮时加载每个UserControl。如果有人有其他解决方案,欢迎。
答案 0 :(得分:1)
您应该访问DockPanel中的'Children'集合
Dock_MainPanel.Children.Clear();
Dock_MainPanel.Children.Add(usr1);
答案 1 :(得分:1)
正如@Stefan W.建议您应该将UserControl
的实例添加到DockPanel的Children集合中,但如果您打算将多个元素添加到同一DockPanel
,您可能还想设置附加的Dock
元素的DockPanel
属性用于指定元素将位于DockPanel.SetDock
的位置。您可以使用DockPanel
方法执行此操作。另外,要使Visibility
可见,请将其Visibility.Visible
设置为private void btn1_Click(object sender, RoutedEventArgs e)
{
Dock_MainPanel.Children.Clear();
Dock_MainPanel.Visibility = Visibility.Visible;
Sample1 usr1 = new Sample1();
DockPanel.SetDock(usr1, Dock.Right);
Dock_MainPanel.Children.Add(usr1);
}
。试试这个:
public static void main(String[] args) throws IOException, InterruptedException
{
WebDriver driver;
System.setProperty("webdriver.chrome.driver", "D:/Application/chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.get("http://www.google.co.in/");
driver.findElement(By.id("lst-ib")).sendKeys("Selenium");
driver.findElement(By.id("_fZl")).click();
Boolean nextButtonFlag = true;
// Create two separate file storing the result
PrintStream searchTitle = new PrintStream(new File(("D:\\Titles.txt")));
PrintStream searchLink = new PrintStream(new File(("D:\\Links.txt")));
do
{
List<WebElement> findElements = driver.findElements(By.xpath("//h3[@class='r']/a"));
for (WebElement element : findElements)
{
// Write all received links and title inn txt file
searchTitle.append(element.getText()+"\n");
searchLink.append(element.getAttribute("href")+"\n");
}
Thread.sleep(2000);
try
{
driver.findElement(By.linkText("Next")).click();
}
catch(Exception e)
{
// no more next button to navigate further link
nextButtonFlag=false;
}
Thread.sleep(2500);
}
while(nextButtonFlag);
System.out.println("Execution done");
searchTitle.close();
searchLink.close();
}
}