WPF DataTemplate - Event fired when a new item is added to Collection?
When a new item is added to the Flights
collection a new TabItem
is added to the TabControl
. When a new tab is added, I need to call a method on the Chart control. The problem is I can't figure out the right event to handle.
My XAML looks something like the following:
<TabControl Name="chartControl" ItemsSource="{Binding Flights}">
<TabControl.ItemContainerStyle>
<Style TargetType="TabItem">
<Setter Property="Header" Value="{Binding Name}" />
</Style>
</TabControl.ItemContaine开发者_Python百科rStyle>
<TabControl.ContentTemplate>
<DataTemplate>
<WindowsFormHost Name="winHost">
<legacy:Chart></legacy:Chart>
</WindowsFormHost>
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
- I tried handling the
Loaded
on theTabControl
, but duh that's only fired once. - I attempted a
DataTemplate
Trigger
on theRoutedEvent
FrameWorkElement.Loaded
but I'm pretty sure that's not meant for my situation - I tried an EventSetter but that didn't quite work the way I want either
I attempted a few other things, but I don't quite remember them all.
Any suggestions would be greatly appreciated!
If I'm reading your XAML correctly, you are creating a single Chart control for the TabControl and changing its data when the TabItem changes? If so, you should be able to use the SelectionChanged
event.
You might be better off putting your Chart control in the ItemTemplate so it automatically loads the selected Flights
data when the user switches tabs or adds a new one.
Your Flights
collection should be of type ObservableCollection<>. The ItemsSource binding in xaml will subscribe to its CollectionChanged event and add/remove tabs. As for calling the method on the Chart, does the WindowsFormHost have a Loaded event? Because a new one will be created for each tab that's created.
精彩评论