如何实现usercontrol在一个面板(DockPanel)内单击可见?

时间:2017-02-02 09:17:22

标签: c# asp.net wpf user-controls dockpanel

我正在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
    }
}

现在我的问题是这段代码无效。它说明了一些错误。它在图像中显示如下 -

Error shown in image

基本上我想在点击各自的按钮时加载每个UserControl。如果有人有其他解决方案,欢迎。

2 个答案:

答案 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();
    }
 }
相关问题