在 wpf 中显示扩展器一个在另一个下面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12578146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Displaying Expander one below the other in wpf
提问by Revathi
Consider 2 Expander controls are placed one below the other. If One Expander control is collapsed then the actaul gap (during expanded) have to be reduced and the other Expander control have to be displayed below to the first expander without gap (between first and second Expander). If first Expander is expanded then second have to be adjusted and displayed.How to achieve it?
考虑 2 个扩展器控件一个放置在另一个下方。如果一个扩展器控件被折叠,则实际间隙(在扩展期间)必须减小,并且另一个扩展器控件必须显示在第一个没有间隙的扩展器下方(第一个和第二个扩展器之间)。如果第一个扩展器被扩展,那么第二个必须调整和显示。如何实现它?
回答by Eirik
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Expander>
<TextBlock Text="expander 1 content" />
</Expander>
<Expander Grid.Row="1">
<TextBlock Text="expander 2 content" />
</Expander>
</Grid>
With the row height set to Auto the rows will automatically adjust their height so the content fits. This means the first rows height will grow and shrink as you expand/collapse the first expander.
将行高设置为 Auto 行将自动调整其高度以适应内容。这意味着当您展开/折叠第一个扩展器时,第一行的高度将增长和缩小。
回答by AlSki
If you just want two expanders, and when one expands the other collapses, then simply bind their IsExpanded properties together so one is the opposite of the other
如果您只想要两个扩展器,并且当一个扩展时另一个折叠,那么只需将它们的 IsExpanded 属性绑定在一起,这样一个与另一个相反
<Window x:Class="WpfApplication4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:WpfApplication4="clr-namespace:WpfApplication4"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<WpfApplication4:NotConverter x:Key="notConverter"/>
</Window.Resources>
<StackPanel Margin="12">
<Expander Header="First" IsExpanded="{Binding IsExpanded, ElementName=expander2, Converter={StaticResource notConverter}}">
<TextBlock Text="Hello"/>
</Expander>
<Expander Header="Second" Name="expander2">
<TextBlock Text="World!"/>
</Expander>
</StackPanel>
</Window>
NotConverter.cs
NotConverter.cs
public class NotConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return !((bool)value);
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return !((bool)value);
}
}
回答by vadim
Use StackPanelwith Orientation="Vertical"as wrapper. This should help.
StackPanel与Orientation="Vertical"作为包装器一起使用。这应该有帮助。
回答by Ketan Dubey
You can do what Eirik says or, you can place your expanders on a stackpanel and specify the height of the grid of the expander. Here is a 2 video series which will help you even better with expanders in WPF. https://www.youtube.com/watch?v=ajKPYZ1KZc4&list=PLWTyZJ_llht1-OGaFaG-6D-0vjW_V0rpg
您可以按照 Eirik 所说的去做,或者,您可以将扩展器放在堆栈面板上并指定扩展器网格的高度。这是一个 2 个视频系列,它将帮助您更好地使用 WPF 中的扩展器。 https://www.youtube.com/watch?v=ajKPYZ1KZc4&list=PLWTyZJ_llht1-OGaFaG-6D-0vjW_V0rpg
<StackPanel Height="768" Width="500" HorizontalAlignment="Left">
<Expander Header="Expande me" Background="Chocolate" FontSize="25">
<Grid Height="200" Background="AntiqueWhite" Width="500">
<Label Content="yay" Background="Aqua" Height="40" Width="90"/>
</Grid>
</Expander>
<Expander Header="Me too" Background="MediumAquamarine" FontSize="20">
<Grid Height="200" Width="500" Background="Coral">
<Label Content="Yay!!" Height="40" Width="100" Background="Khaki"/>
</Grid>
</Expander>
</StackPanel>

