WPF TabControl 子级

发布于 2024-10-23 20:29:29 字数 2480 浏览 10 评论 0原文

这是我当前的场景:我在 WPF 窗口中的单个 TabControl 上的不同 TabItem 内有多个 UserControl。比如:

<Window x:Class="MainWindow"  
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
Title="S.C.A.R" WindowState="Maximized">  
    <TabControl Name="MainTabControl">  
        <TabItem Name="TabOps">  
            <Grid>  
                <Grid.RowDefinitions>  
                    <RowDefinition/>  
                    <RowDefinition/>  
                    <RowDefinition/>  
                    <RowDefinition Height="20"/>  
                </Grid.RowDefinitions>  
                <Grid.ColumnDefinitions>  
                    <ColumnDefinition/>  
                    <ColumnDefinition Width="30"/>  
                    <ColumnDefinition/>  
                </Grid.ColumnDefinitions>  
                <Local:ServiceList Height="Auto" CanInsert="True" CanCollapse="True" Grid.ColumnSpan="3" x:Name="SL" RecordState="Edit"/>  
                <Local:ReservationList CanCollapse="True" Grid.Row="1"  RecordState="Edit" x:Name="RL"/>  
                <Local:DriverList CanDelete="False" CanInsert="False"   CanCollapse="True" Grid.Row="1" Grid.Column="2" RecordState="Edit" x:Name="DL"/>  
                <Local:CustomerForm CanDelete="False" CanInsert="False" Grid.Row="2"   Grid.ColumnSpan="3" RecordState="View" x:Name="CL"/>  
                </Grid>  
        </TabItem>  
        <TabItemItem Name="TabCodes">  
                <Local:CustomerList x:Name="CustomerCRUD" RecordState="View"/>  
         </TabItem>  
        <Button Grid.Row="1"  Content="TEST" Click="Button_Click"/>  
    </Grid>  
</Border>  
</Window>  

抱歉缩进。由于某种原因,我无法在此处正确缩进代码:(

我需要做的是确定(最好在 TabControl.Load 方法中,我的不同 UserControl 中的哪些当前可见。我需要以动态方式执行此操作,我无法对 TabItem 及其子项之间的关系进行硬编码,例如: if (TabControl.SelectedItem is XXXX)... 在这里是不可能的,因为这是一个动态 UI,我无法知道前面有哪些控件。

我进行了一些挖掘,发现 TabItem 控件不会出现在其“子项”的可视化树中。我只看到 ContentPresenter,然后是 TabControl 本身。看起来 tabItems 不“包含”自己的内容,因此我无法对选项卡项执行 FindAncestor 等操作。

另一个有趣的事实是我的用户控件的 Loaded 事件在启动时被调用。无论它们是否在屏幕上可见。

理想的情况是找到一个仅在用户控件所在的 TabItem 被选中时才在用户控件上触发的事件。

欣赏任何想法。提前致谢

This is my current Scenario: I have several UserControls inside different TabItems on a single TabControl in a WPF Window. Something Like:

<Window x:Class="MainWindow"  
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
Title="S.C.A.R" WindowState="Maximized">  
    <TabControl Name="MainTabControl">  
        <TabItem Name="TabOps">  
            <Grid>  
                <Grid.RowDefinitions>  
                    <RowDefinition/>  
                    <RowDefinition/>  
                    <RowDefinition/>  
                    <RowDefinition Height="20"/>  
                </Grid.RowDefinitions>  
                <Grid.ColumnDefinitions>  
                    <ColumnDefinition/>  
                    <ColumnDefinition Width="30"/>  
                    <ColumnDefinition/>  
                </Grid.ColumnDefinitions>  
                <Local:ServiceList Height="Auto" CanInsert="True" CanCollapse="True" Grid.ColumnSpan="3" x:Name="SL" RecordState="Edit"/>  
                <Local:ReservationList CanCollapse="True" Grid.Row="1"  RecordState="Edit" x:Name="RL"/>  
                <Local:DriverList CanDelete="False" CanInsert="False"   CanCollapse="True" Grid.Row="1" Grid.Column="2" RecordState="Edit" x:Name="DL"/>  
                <Local:CustomerForm CanDelete="False" CanInsert="False" Grid.Row="2"   Grid.ColumnSpan="3" RecordState="View" x:Name="CL"/>  
                </Grid>  
        </TabItem>  
        <TabItemItem Name="TabCodes">  
                <Local:CustomerList x:Name="CustomerCRUD" RecordState="View"/>  
         </TabItem>  
        <Button Grid.Row="1"  Content="TEST" Click="Button_Click"/>  
    </Grid>  
</Border>  
</Window>  

Sorry for the indentation. For some reason I can't get the code properly indented here :(

What I need to do is to determine (preferably in the TabControl.Load Method, which of my different UserControls are currently visible. I need to do this in a dynamic way, I cannot hardcode the relationship between the TabItems and their children, something like:
if (TabControl.SelectedItem is XXXX)... is not possible here, because this is a Dynamic UI and I have no way to know which controls are there up front.

I've been digging a little bit and found out that the TabItem controls do not appear in the Visual tree of their "children". I only see a ContentPresenter, and then the TabControl itself. It looks like the tabItems do not "contain" their own content, so I could not, for example, do a FindAncestor to the Tab Items.

Another interesting fact is that the Loaded event of my usercontrols is being called on startup. Regardless of whether or not they're visible on screen.

An ideal scenario will be to find an event that is only fired on my Usercontrols when the TabItem they are under gets selected.

Appreciate any ideas. Thanks in advance

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

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

发布评论

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

评论(1

苍景流年 2024-10-30 20:29:29

您应该能够利用 VisualTreeHelper 并因此 answer 提供 TabItem.Content 返回的对象,并在此实例中查找您指定的类型 UserControl

注意

有关更多详细信息,请参阅 SO 问题中的评论。

You should be able to leverage the VisualTreeHelper and consequentrly this answer on SO to provide the TabItem.Content returned object and look for the your specified type, UserControl in this instance.

NOTE:

For additional details please see the comments which transpired in the SO's question.

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