wpf 组合扩展器和网格(可调整大小的扩展器)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5553383/
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
Combine expander and grid (resizable expander)
提问by Daniel Bi?ar
I would like to have something like a resizable Expander. My basic idea was something like this:
我想要一个可调整大小的扩展器。我的基本想法是这样的:
<Grid HorizontalAlignment="Left">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="2" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Expander Grid.Column="0" ExpandDirection="Right">
...
</Expander>
<GridSplitter Grid.Column="1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch" />
...
</Grid>
The problem with this: if i move the grid splitter and collaps the expander i got a big empty area. How can make the entire column collapse? Or is there another way to make the expander "resizable"
问题在于:如果我移动网格分离器并折叠扩展器,我会得到一个很大的空白区域。怎样才能让整列倒塌?或者是否有另一种方法可以使扩展器“可调整大小”
回答by H.B.
Not sure what you are trying to accomplish but i think conceptually the Grid
should be part of the Expander.Content
, would this work for you?
不确定你想要完成什么,但我认为从概念上讲Grid
应该是 的一部分Expander.Content
,这对你有用吗?
<Expander Header="Test" ExpandDirection="Right" HorizontalAlignment="Left" Background="LightBlue">
<Expander.Content>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="5"/>
</Grid.ColumnDefinitions>
<TextBlock Text="Lorem ipsum dolor sit"/>
<GridSplitter Grid.Column="1" Width="5" ResizeBehavior="PreviousAndCurrent" ResizeDirection="Columns"/>
</Grid>
</Expander.Content>
</Expander>
Edit:Removed all the triggering from the first column as it seemed unnecessary.
编辑:从第一列中删除了所有触发,因为它似乎没有必要。
Also: For this to work vertically the GridSplitter's HorizontalAlignment
must be set to Stretch
, otherwise it will have zero width by default (of course everything else that is orientation-specific must be adapted as well but that is straightforward)
另外:为了使其垂直工作,HorizontalAlignment
必须将GridSplitter设置为Stretch
,否则默认情况下它将具有零宽度(当然,其他所有特定于方向的内容也必须进行调整,但这很简单)
HorizontalAlignment is the Microsoft .NET property accessor for what is in reality a dependency property. This particular dependency property quite frequently has its apparent "default" value set differently in subclassed elements, particularly controls. [...] For example, the apparent "default" of HorizontalAlignment for a Label control will be Left, even though Label inherits HorizontalAlignment direct from FrameworkElement. This is because that value was reset within the default style of Label, within the style's control template.
HorizontalAlignment 是 Microsoft .NET 属性访问器,它实际上是一个依赖属性。这个特定的依赖属性经常在子类元素(特别是控件)中设置不同的明显“默认”值。[...] 例如,即使 Label 直接从 FrameworkElement 继承 HorizontalAlignment,Label 控件的 HorizontalAlignment 的明显“默认值”也是 Left。这是因为该值在 Label 的默认样式中,在样式的控件模板中被重置。
回答by Sonorx
Maybe this will help to solve your "column collapse" problem
也许这将有助于解决您的“列崩溃”问题
XAML:
XAML:
Add in <Grid>
Name="expGrid"
and add in <Expander>
Collapsed="Expander_Collapsed"
添加<Grid>
Name="expGrid"
并添加<Expander>
Collapsed="Expander_Collapsed"
C# Code:
C# 代码:
private void Expander_Collapsed(object sender, RoutedEventArgs e)
{
var colomnIndex = Grid.GetColumn(sender as Expander);
var colomn = expGrid.ColumnDefinitions[colomnIndex];
colomn.Width = GridLength.Auto;
}