简体   繁体   中英

Using WPF TabControl's ItemsSource property makes the Parent property of its child null

I'm binding WPF TabControl's ItemsSource property to an ObservableCollection of TabItem.

XAML:

<TabControl ItemsSource={Binding TabItems}/>

C#:

public ObservableCollection<TabItem> TabItems {get; set;}

With this model, the Parent property of TabItem is always null. However, If I use Items property of TabControl and add the TabItems, the parent property of TabItem in the TabControl is not null. Not sure why it is so.

The parent property returns null because the TabItem is not the child of the TabControl. One reason for this to fail is inapropriate DataContext s, so the XAML Code <TabControl ItemsSource={Binding TabItems}/> could fail.

Check if the DataContexts are proper. If you are having trouble in checking the DataContext , try Snoop, a handy program every WPF developer must have :) Snoop

Start Snoop, ask it to 'Snoop' your running program, a window appears with Visual Hierarchy on the left. Drill down to your TabControl and check its DataContext on the right (Its the second tab item, RightTop). Verify it shows your property you defined TabItems ( public ObservableCollection<TabItem> TabItems {get; set;} )

Hope it helps :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM