WPF:在扩展器中设置DataGrid的大小 [英] WPF: Set Size of an DataGrid in a Expander
问题描述
我有一个TabControl,其中包含带有许多Expander的StackPanel。在每个扩展器中,我放置了一个UserControl。所以,我有一个清晰的看法。
UserControl包含一个DataGrid。现在的问题是,未设置DataGrid的高度。如果DataGrid大于窗口大小,则不会显示滚动条。
I have a TabControl which contains a StackPanel with many Expander. In each Expander I placed an UserControl. So, I have a clear view. The UserControl contains a DataGrid. Now, the problem is, that the height from the DataGrid is not set. If the DataGrid is lager than the window-size no Scrollbar is shown.
<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中设置高度,则会显示滚动条,但它不是动态的。
If I tried to set the height in the UserControl a Scrollbar is shown, but it is not dynamic.
<view:Misc2View .... height="800" ... />
更新:我已经尝试使用Binding设置高度:
Update: I already tried to set the height with Binding:
Height="{Binding ElementName=Misc2Expander, Path=Height}"
推荐答案
内部 DataGrid
的高度
您的 UserControl
不会受到限制,直到您明确设置一个 height
为止。
The height
of the DataGrid
inside your UserControl
is not restricted until you set a fix height
explicitly.
<DataGrid Height="300" ... />
这将显示滚动条
。
编辑:
要避免显式的高度,可以使用 Grid
使用 RowDefinitions
而不是 StackPanel
,然后绑定 DataGrid高度
到 Expander.ActualHeight
像这样:
To avoid explicit height, you can use Grid
with RowDefinitions
instead of StackPanel
and then bind DataGrid.Height
to Expander.ActualHeight
like this:
MainWindow:
MainWindow:
<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>
UserControl:
UserControl:
<DataGrid Height="{Binding RelativeSource={RelativeSource AncestorType=Expander}, Path=ActualHeight}" ... />
第二次编辑
如果我正确理解了您的问题,则需要为 RowDefinitions
设置触发器,以设置当前的 Height
将 Expander
扩展为 *
,而将其他<$$的 Height
c $ c> Expanders 保持 Auto
像这样:
If I understand your issue correctly, then you need to set triggers for RowDefinitions
to set the Height
of currently expanded Expander
to *
while the Height
of other Expanders
remain Auto
like this:
<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 $ c上设置数据绑定$ c>而不是
DataGrid.Height
在您的 UserControl
中的其他元素可见:
You might want to set data binding on UserControl.Height
instead of DataGrid.Height
for other elements in your UserControl
to be visible:
<UserControl x:Class="view:Misc2View" ...
Height="{Binding RelativeSource={RelativeSource AncestorType=Expander},
Path=ActualHeight}" ... />
这篇关于WPF:在扩展器中设置DataGrid的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!