我有一个包含许多扩展一个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}"
该height
的的DataGrid
的内部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
设置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}" ... />