在创建TabItem之后在TabItem中获取控件

时间:2016-10-30 18:55:01

标签: c# wpf tabs wpf-controls

我试图获取一个新创建的标签(基于模板创建)TextBlock控件,但我尝试过的所有方法都返回null。

XAML:

//This is first called in the constructor of the Window class, after InitializeComponent ()
TabItem newPage = new TabItem ();
newPage.Header = "new tab";
messageTabControl.Items.Add (newPage);
messageTabControl.SelectedIndex = messageTabControl.Items.Count - 1;
TabItem createdPage = (TabItem)messageTabControl.Items[messageTabControl.Items.Count - 1];

int childrenCount = VisualTreeHelper.GetChildrenCount (createdPage); //Returns 0

ScrollViewer scroll;
TextBlock textBlock;

//All of these return null
textBlock = (TextBlock)createdPage.FindName ("messageTabTextBlock");
textBlock = (TextBlock)LogicalTreeHelper.FindLogicalNode (createdPage, "messageTabTextBlock");
scroll = (ScrollViewer)createdPage.FindName ("Scroll");
scroll = (ScrollViewer)LogicalTreeHelper.FindLogicalNode (createdPage, "Scroll");

CS:

#[macro_use]
extern crate nom;

use nom::*;

named!(string< Vec<u8> >, delimited!(
    tag!("@"),
    fold_many0!(
        alt!(
            is_not!(b"@") |
            map!(
                complete!(tag!("@@")),
                |_| &b"@"[..]
            )
        ),
        Vec::new(),
        |mut acc: Vec<u8>, bytes: &[u8]| {
            acc.extend(bytes);
            acc
        }
    ),
    tag!("@")
));

#[test]
fn it_works() {
    assert_eq!(string(b"@string@"), IResult::Done(&b""[..], b"string".to_vec()));
    assert_eq!(string(b"@string with @@ escapes@"), IResult::Done(&b""[..], b"string with @ escapes".to_vec()));
    assert_eq!(string(b"@invalid string"), IResult::Incomplete(Needed::Size(16)));
}

0 个答案:

没有答案