WPF:在扩展的数据网格的设置尺寸(WPF: Set Size of an DataGrid in

2019-10-23 11:26发布

我有一个包含许多扩展一个StackPanel一个TabControl。 在每个扩展我放置在用户控件。 所以,我有一个明确的说法。 该用户控件包含一个DataGrid。 现在的问题是,从DataGrid高度未设置。 如果DataGrid是啤酒比窗口大小显示没有滚动条。

<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>

如果我试图设置在用户控件滚动条显示的高度,但它不是动态的。

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

更新:我已经尝试设置高度与绑定:

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

Answer 1:

height的的DataGrid的内部UserControl没有限制,直到您设置一个固定height明确。

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

这将使Scrollbar可见。

编辑:

为了避免明确的高度,你可以使用GridRowDefinitions代替StackPanel ,然后绑定DataGrid.HeightExpander.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设置Height当前扩大Expander*Height等的Expanders保持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}" ... />


文章来源: WPF: Set Size of an DataGrid in a Expander