我GridSplitter在我的WPF应用程序中使用了一个,并希望从代码中将其设置为特定的初始位置.我一直坚持GridSplitter将特定值设置为Height关联属性时的行为RowDefinition.
这是我举例说明这个问题.我已添加TextBlocks以显示RowDefinition.Height属性的实际值.
<Grid>
<Grid.RowDefinitions>
<RowDefinition x:Name="RowDef1" Height="*" MinHeight="20"/>
<RowDefinition Height="Auto" />
<RowDefinition x:Name="RowDef2" Height="*" MinHeight="20"/>
</Grid.RowDefinitions>
<DockPanel Grid.Row="0" LastChildFill="True" >
<TextBlock FontSize="16" VerticalAlignment="Top" Text="Upside Height: "/>
<TextBlock x:Name="Tbl1" FontSize="16" Text="{Binding Height}"/>
</DockPanel>
<GridSplitter Grid.Row="1" Height="10" HorizontalAlignment="Stretch" ResizeBehavior="PreviousAndNext" Background="Black" />
<DockPanel Grid.Row="2" LastChildFill="True" >
<TextBlock FontSize="16" VerticalAlignment="Top" Text="Downside Height: "/>
<TextBlock x:Name="Tbl2" FontSize="16" Text="{Binding Height}"/>
</DockPanel>
</Grid>
Run Code Online (Sandbox Code Playgroud)
namespace WpfGridSplitterTest
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
Tbl1.DataContext = this.RowDef1;
Tbl2.DataContext = this.RowDef2;
}
}
}
Run Code Online (Sandbox Code Playgroud)
结果按预期工作.GridSplitter调整窗口的两个部分的大小,服从MinHeight值并停在窗口边界.调整窗口大小也按设计工作:-)
现在我正试图设置代码GridSplitter隐藏的初始位置,如下所示:
InitializeComponent();
RowDef1.Height = new GridLength(50, GridUnitType.Pixel);
Tbl1.DataContext = this.RowDef1;
Tbl2.DataContext = this.RowDef2;
Run Code Online (Sandbox Code Playgroud)
结果是weired:
GridSplitter最初设置为50像素从上(这很好)MinHeight20px 的上排(也没关系)但
MinHeight下排的值ActualHeight 移动GridSplitter时,不再更新下行的值我在这里错过了什么?任何提示都非常感谢!
我已阅读这篇文章,但答案在我的情况下不起作用: 如何设置GridSplitter'窗格'的初始高度?
更改Height="*"到Height="Auto"也不会被解决了这个问题.
问题是你将RowDef1的高度设置为50 PIXEL.你想要做的是将它设置为STAR.但是明星有什么价值?
由于您希望RowDef1最终为50像素,因此您需要计算RowDef1/2的可用空间(以像素为单位).以STAR为单位为RowDef1分配50,并以STAR为单位将剩余分配给RowDef2.
InitializeComponent();
Tbl1.DataContext = this.RowDef1;
Tbl2.DataContext = this.RowDef2;
this.Loaded += (s, e) =>
{
var height = RowDef1.ActualHeight + RowDef2.ActualHeight; // 301 pixels
RowDef1.Height = new GridLength(50, GridUnitType.Star); // 50*
RowDef2.Height = new GridLength(height - 50, GridUnitType.Star); // 251*
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4264 次 |
| 最近记录: |