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

时间:2015-06-23 08:42:35

标签: c# wpf datagrid user-controls expander

我有一个TabControl,其中包含一个带有许多Expander的StackPanel。在每个Expander中,我放置了一个UserControl。所以,我有一个清晰的观点。 UserControl包含DataGrid。现在,问题是,没有设置DataGrid的高度。如果DataGrid大于窗口大小,则不显示Scrollbar。

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

1 个答案:

答案 0 :(得分:2)

heightDataGrid的{​​{1}}不受限制,直到您明确设置修复UserControl为止。

height

这会使<DataGrid Height="300" ... /> 可见。

修改

要避免明确的身高,您可以Scrollbar使用Grid代替RowDefinitions,然后将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的{​​{1}}设置为Height Expander其他*的{​​{1}}仍为此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