WPF:在扩展器中设置DataGrid的大小
本文关键字:DataGrid 设置 扩展器 WPF | 更新日期: 2023-09-27 17:59:11
我有一个TabControl,它包含一个带有许多扩展器的StackPanel。在每个扩展器中,我放置了一个UserControl。所以,我有一个清晰的观点。UserControl包含一个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>
如果我试图在UserControl中设置高度,会显示滚动条,但它不是动态的。
<view:Misc2View .... height="800" ... />
更新:我已经尝试使用Binding:设置高度
Height="{Binding ElementName=Misc2Expander, Path=Height}"
在显式设置修复height
之前,UserControl
中DataGrid
的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
设置触发器,以将当前扩展的Expander
的Height
设置为*
,而其他Expanders
的Height
保持为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}" ... />