WPF DataTemplate - 将新项目添加到集合时触发事件?

发布于 2024-10-01 17:23:20 字数 1108 浏览 2 评论 0原文

当新项目添加到 Flights 集合中时,新的 TabItem 也会添加到 TabControl 中。添加新选项卡时,我需要调用图表控件上的方法。问题是我无法找出要处理的正确事件。

我的 XAML 如下所示:

<TabControl Name="chartControl" ItemsSource="{Binding Flights}">
    <TabControl.ItemContainerStyle>
        <Style TargetType="TabItem">
            <Setter Property="Header" Value="{Binding Name}" />
        </Style>
    </TabControl.ItemContainerStyle>
    <TabControl.ContentTemplate>
        <DataTemplate>
            <WindowsFormHost Name="winHost">
                <legacy:Chart></legacy:Chart>
            </WindowsFormHost>
        </DataTemplate>
    </TabControl.ContentTemplate>
</TabControl>
  • 我尝试处理 TabControl 上的 Loaded, 但是只触发了一次。
  • 我尝试了DataTemplate RoatedEvent 上的触发器
    FrameWorkElement.Loaded 但我很确定这不适合我的情况
  • 我尝试了 EventSetter 但那 不太按照我想要的方式工作 或者

我尝试了一些其他的事情,但我不太记得它们了。

任何建议将不胜感激!

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.ItemContainerStyle>
    <TabControl.ContentTemplate>
        <DataTemplate>
            <WindowsFormHost Name="winHost">
                <legacy:Chart></legacy:Chart>
            </WindowsFormHost>
        </DataTemplate>
    </TabControl.ContentTemplate>
</TabControl>
  • I tried handling the Loaded on the TabControl,
    but duh that's only fired once.
  • I attempted a DataTemplate
    Trigger on the RoutedEvent
    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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一口甜 2024-10-08 17:23:20

如果我正确读取您的 XAML,您是否正在为 TabControl 创建单个图表控件并在 TabItem 更改时更改其数据?如果是这样,您应该能够使用 SelectionChanged 事件。

您最好将 Chart 控件放在 ItemTemplate 中,这样当用户切换选项卡或添加新选项卡时,它会自动加载选定的 Flights 数据。

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.

冷夜 2024-10-08 17:23:20

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文