public MainWindow() { InitializeComponent(); for (int i = 0; i < 20; i++) { MyListbox.Items.Add("items" + i); } var fStack = new FrameworkElementFactory(typeof(StackPanel), "tStack"); var fBorder = new FrameworkElementFactory(typeof(Border), "tBoder"); fBorder.SetValue(Border.BackgroundProperty, new SolidColorBrush(Colors.Red)); fBorder.SetValue(Border.WidthProperty, 20.0); fBorder.SetValue(HeightProperty, 20.0); fStack.AppendChild(fBorder); var dt = new DataTemplate(typeof(StackPanel)); dt.VisualTree = fStack; MyListbox.ItemTemplate = dt; var neko = MyListbox.ApplyTemplate(); var ts = dt.FindName("tStack", MyListbox); LayoutUpdated += MainWindow_LayoutUpdated; Loaded += MainWindow_Loaded; }
private void MainWindow_LayoutUpdated(object sender, EventArgs e) { MyListbox.ApplyTemplate(); //var tst = MyListbox.ItemTemplate.FindName("tStack",this); }