WPF C#.CS .Name未在通话时读取

时间:2015-02-28 06:30:12

标签: c# wpf

我的边框上没有读取我的.Name。我怎么能读它?这是代码:

        Border Border1 = new Border();
        Border1.Name = "_webBrowserPlacementTarget";

        WebBrowserOverlayWF wbo = new WebBrowserOverlayWF(_webBrowserPlacementTarget);

我在_webBrowserPlacementTarget上得到一条红线。代码也不会被执行,因此程序永远不会形成。我尝试使用此代码:

        public FrameworkElement _webBrowserPlacementTarget { get; set; }

但是窗口不会加载。我如何阅读.Name?

2 个答案:

答案 0 :(得分:0)

这里的简单示例,可能有帮助:

yourPanel是StackPanel

        Border Border1 = new Border();
        Border1.Name = "_webBrowserPlacementTarget";

        yourPanel.Children.Add(Border1);

        var children = (yourPanel as Panel).Children;
        Border border = (from Border child in children
                         where child.Name == "_webBrowserPlacementTarget"
                         select child).First();

修改

        Border Border1 = new Border();
        Border1.Name = "_webBrowserPlacementTarget";

        yourPanel.Children.Add(Border1);

        this.RegisterName(Border1.Name, Border1);
        var border = this.FindName("_webBrowserPlacementTarget");

谢谢!

答案 1 :(得分:0)

您可以直接使用border元素而无需注册名称

WebBrowserOverlayWF wbo = new WebBrowserOverlayWF(Border1);