wpf TabControl在stackpanel内部不工作

fhity93d  于 2023-08-07  发布在  其他
关注(0)|答案(2)|浏览(180)

因此,我有一个TabControl,它通常驻留在TabItem中。我想把这个选项卡控件放在一个StackPanel中,这样我就可以在TabControl上面添加一些内容。然而,一旦我将TabControl放入StackPanel,应用程序就会停止工作--某处出现内存泄漏,如果我在Windows任务管理器中查看我的进程,它就会一直使用越来越多的内存,直到需要关闭。我的. xaml文件非常简单:

<TabControl Name="tabControlOuter" Margin="0,20,0,0" Background="#222222" >
<TabItem>...</TabItem> 
<TabItem>...</TabItem> 
<TabItem Header="Something" Name="something">
    <TabControl Name="tabControlInner" Style="{StaticResource TabControlStyle1}"/>
</TabItem>

字符串
我的选项卡控件样式:

<Style x:Key="TabControlStyle1" TargetType="{x:Type TabControl}">
        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.ControlTextBrushKey}}"/>
        <Setter Property="Padding" Value="4,4,4,4"/>
        <Setter Property="BorderThickness" Value="1"/>
        <Setter Property="BorderBrush" Value="Black"/>
        <Setter Property="Background" Value="#222222"/>
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="VerticalContentAlignment" Value="Center"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type TabControl}">
                    <Grid x:Name="ctlgrid" ClipToBounds="true" SnapsToDevicePixels="true" KeyboardNavigation.TabNavigation="Local">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition x:Name="ColumnDefinition0"/>
                            <ColumnDefinition x:Name="ColumnDefinition1" Width="0"/>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition x:Name="RowDefinition0" Height="Auto"/>
                            <RowDefinition x:Name="RowDefinition1" Height="*"/>
                        </Grid.RowDefinitions>
                        <Grid Panel.ZIndex="1">
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="*"/>
                                <ColumnDefinition Width="Auto"/>
                            </Grid.ColumnDefinitions>
                            <WrapPanel x:Name="HeaderPanel" Grid.Column="0" IsItemsHost="true" Background="#666666" Grid.Row="0" KeyboardNavigation.TabIndex="1" Panel.ZIndex="1"/>
                            <DockPanel Width="300" x:Name="SearchDock" Background="#666666" Grid.Column="1" Height="65">
                                <Border BorderBrush="White" BorderThickness="1,0,1,0" HorizontalAlignment="Right" Margin="0,0,10,0"></Border>

                                <Label x:Name="searchStat" Height="30" DockPanel.Dock="Bottom" Foreground="White"></Label>
                                <TextBox x:Name="Search" Margin="0,0,10,0" Width="150" Height="30" DockPanel.Dock="Left" />

                                <Button ToolTip="Search" Width="35" Height="30" Padding="5,5,5,2" Margin="10,0,-30,0" Click="search_logs" DockPanel.Dock="Left" >
                                    <Image Source="/Images/mglass.png" />
                                </Button>
                                <Button ToolTip="Next Search Result" Margin="0,0,20,0" Width="30" Height="30" Content="Next" Click="next_result" DockPanel.Dock="Right">
                                </Button>
                                <Button ToolTip="Previous Search Result" Margin="30,0,0,0" Width="30" Height="30" Content="Prev" Click="prev_result" DockPanel.Dock="Right">
                                </Button>

                            </DockPanel>

                        </Grid>

                        <Border x:Name="ContentPanel" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Grid.Column="0" KeyboardNavigation.DirectionalNavigation="Contained" Grid.Row="1" KeyboardNavigation.TabIndex="2" KeyboardNavigation.TabNavigation="Local">
                            <ContentPresenter x:Name="PART_SelectedContentHost" ContentSource="SelectedContent" Margin="{TemplateBinding Padding}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                        </Border>


                    </Grid>
                    <ControlTemplate.Triggers>
                        <Trigger Property="TabStripPlacement" Value="Left">
                            <Setter Property="Grid.Row" TargetName="HeaderPanel" Value="0"/>
                            <Setter Property="Grid.Row" TargetName="ContentPanel" Value="0"/>
                            <Setter Property="Grid.Column" TargetName="HeaderPanel" Value="0"/>
                            <Setter Property="Grid.Column" TargetName="ContentPanel" Value="1"/>
                            <Setter Property="Width" TargetName="ColumnDefinition0" Value="Auto"/>
                            <Setter Property="Width" TargetName="ColumnDefinition1" Value="*"/>
                            <Setter Property="Height" TargetName="RowDefinition0" Value="*"/>
                            <Setter Property="Height" TargetName="RowDefinition1" Value="0"/>                               
                        </Trigger>

                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>


现在的问题是,如果我在我的内部选项卡控件周围使用StackPanel,事情就会变得很糟糕

<TabItem Header="Something" Name="something">
<StackPanel>
    <TabControl Name="tabControlInner" Style="{StaticResource TabControlStyle1}"/>
<StackPanel>
</TabItem>


我以编程方式将选项卡项添加到我的选项卡控件中,如下所示:

TabControl itemsTab = (TabControl)this.FindName("tabControlInner");
            itemsTab.Items.Clear();
            TabItem setThistab = (TabItem)this.FindName("something");
            setThistab.IsSelected = true;
            foreach (CustomItem ale in my_collection)
            {
                TabItem newTab = new TabItem();
                FrameDisplayTab ftab1 = new FrameDisplayTab();
                ftab1.MyDatagrid.ItemsSource = ale.logentries;
                newTab.Content = ftab1;
                newTab.Header = ale.name;
                itemsTab.Items.Add(newTab); <-- this is where I get into trouble from the stack pane. Everything works fine if my inner tab control is not in a stack panel or if I omit this line

            }


有人能看到发生了什么吗??任何地方都不会掷回例外状况。

frebpwbc

frebpwbc1#

对于任何遇到这个问题的人来说,看起来StackPanel就是问题所在。看起来除非你在StackPanel上指定宽度和高度,否则它和它的子元素的高度和宽度都是NaN,这就是导致问题的原因。因为我希望高度和宽度是'拉伸',我不能使用堆栈面板。
为了解决这个问题,我将StackPanel改为DockPanel。问题解决了

vhipe2zx

vhipe2zx2#

您可以使用Grid或DockPanel来解决Stackpanel的这个问题。下面是伪代码,它使用Grid在TabItems上容纳Menu。

<Window x:Class="WpfApp.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="WPF App" Height="450" Width="800">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/> <!-- Menu Row -->
        <RowDefinition Height="*"/>    <!-- TabItems Row -->
    </Grid.RowDefinitions>

    <!-- Menu -->
    <Menu Grid.Row="0">
        <!-- Add your menu items here -->
        <MenuItem Header="File">
            <MenuItem Header="Open"/>
            <MenuItem Header="Save"/>
        </MenuItem>
        <MenuItem Header="Edit">
            <MenuItem Header="Cut"/>
            <MenuItem Header="Copy"/>
            <MenuItem Header="Paste"/>
        </MenuItem>
    </Menu>

    <!-- TabControl with TabItems -->
    <TabControl Grid.Row="1">
        <!-- Add your TabItems here -->
        <TabItem Header="Tab 1">
            <!-- Tab 1 Content -->
        </TabItem>
        <TabItem Header="Tab 2">
            <!-- Tab 2 Content -->
        </TabItem>
        <TabItem Header="Tab 3">
            <!-- Tab 3 Content -->
        </TabItem>
    </TabControl>
</Grid>
</Window>

字符串

相关问题