WPF:在扩展器中设置 DataGrid 的大小

WPF: Set Size of an DataGrid in a Expander

我有一个 TabControl,它包含一个带有许多扩展器的 StackPanel。在每个扩展器中,我都放置了一个 UserControl。所以,我有一个明确的看法。 UserControl 包含一个 DataGrid。现在,问题是未设置 DataGrid 的高度。如果 DataGrid 大于 window 大小,则不显示滚动条。

<TabControl SelectionChanged="Selector_OnSelectionChanged" Height="Auto">
        <TabItem Header="DoSmth">
        </TabItem>
        <TabItem Header="Misc" Height="Auto">
            <StackPanel Height="Auto">
                <Expander Header="Misc1" IsExpanded="False" Margin="0,10,0,0">
                </Expander>          
                <Expander Header="Misc2" IsExpanded="False" x:Name="Misc2Expander" Expanded="ExpanderMisc2_OnExpanded" Height="Auto" Margin="0,10,0,0">
                    <view:Misc2View Background="WhiteSmoke" Width="Auto" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" HorizontalContentAlignment="Left" VerticalContentAlignment="Top"/>
                </Expander>                    
            </StackPanel>
        </TabItem>            
    </TabControl>

如果我尝试在 UserControl 中设置高度,则会显示一个滚动条,但它不是动态的。

<view:Misc2View .... height="800" ... />

更新:我已经尝试使用 Binding 设置高度:

Height="{Binding ElementName=Misc2Expander, Path=Height}"

您的 UserControlDataGridheight 不受限制,直到您明确设置修复 height

<DataGrid Height="300" ... />

这将使 Scrollbar 可见。

编辑:

为了避免显式高度,您可以使用 GridRowDefinitions 而不是 StackPanel,然后像这样将 DataGrid.Height 绑定到 Expander.ActualHeight

主窗口:

    <TabControl >
        <TabItem Header="DoSmth">
        </TabItem>
        <TabItem Header="Misc" >
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <Expander Grid.Row="0" Header="Misc1" IsExpanded="False" Margin="0,10,0,0">
                </Expander>
                <Expander Grid.Row="1"
                    Header="Misc2" IsExpanded="False" x:Name="Misc2Expander" Margin="0,10,0,0">
                    <view:Misc2View Background="WhiteSmoke"/>
                </Expander>
            </Grid>
        </TabItem>
    </TabControl>

用户控件:

<DataGrid Height="{Binding RelativeSource={RelativeSource AncestorType=Expander}, Path=ActualHeight}" ... />

第二次编辑

如果我对您的问题的理解正确,那么您需要为 RowDefinitions 设置触发器以将当前扩展的 ExpanderHeight 设置为 * 而 [=其他 Expanders 的 26=] 像这样保持 Auto

    <TabControl SelectionChanged="Selector_OnSelectionChanged" Grid.Row="0" >
        <TabItem Header="DoSmth">
        </TabItem>
        <TabItem Header="Misc" >
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Name="GridRow1">
                        <RowDefinition.Style>
                            <Style TargetType="{x:Type RowDefinition}">
                                <Setter Property="Height"  Value="Auto" />
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding ElementName=Misc1Expander,
                                        Path=IsExpanded}" Value="True">
                                        <Setter Property="Height" Value="*" />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </RowDefinition.Style>
                    </RowDefinition>
                    <RowDefinition Name="GridRow2">
                        <RowDefinition.Style>
                            <Style TargetType="{x:Type RowDefinition}">
                                <Setter Property="Height"  Value="Auto" />
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding ElementName=Misc2Expander,
                                        Path=IsExpanded}" Value="True">
                                        <Setter Property="Height" Value="*" />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </RowDefinition.Style>
                    </RowDefinition>
                    <RowDefinition Name="GridRow3">
                        <RowDefinition.Style>
                            <Style TargetType="{x:Type RowDefinition}">
                                <Setter Property="Height"  Value="Auto" />
                                <Style.Triggers>
                                    <DataTrigger Binding="{Binding ElementName=Misc3Expander,
                                        Path=IsExpanded}" Value="True">
                                        <Setter Property="Height" Value="*" />
                                    </DataTrigger>
                                </Style.Triggers>
                            </Style>
                        </RowDefinition.Style>
                    </RowDefinition>
                </Grid.RowDefinitions>
                <Expander Grid.Row="0"
                    Header="Misc2" IsExpanded="False" x:Name="Misc1Expander" Margin="0,10,0,0">
                    <view:Misc2View Background="WhiteSmoke"/>
                </Expander>
                <Expander Grid.Row="1"
                    Header="Misc2" IsExpanded="False" x:Name="Misc2Expander" Margin="0,10,0,0">
                    <view:Misc2View Background="WhiteSmoke"/>
                </Expander>
                <Expander Grid.Row="2"
                    Header="Misc2" IsExpanded="False" x:Name="Misc3Expander" Margin="0,10,0,0">
                    <view:Misc2View Background="WhiteSmoke"/>
                </Expander>
            </Grid>
        </TabItem>
    </TabControl>

您可能希望在 UserControl.Height 而不是 DataGrid.Height 上设置数据绑定,以使 UserControl 中的其他元素可见:

<UserControl x:Class="view:Misc2View" ...
             Height="{Binding RelativeSource={RelativeSource AncestorType=Expander}, 
             Path=ActualHeight}" ... />