![](/img/trans.png)
[英]How to set max width for a grid in a XAML view to the max dialog width?
[英]How to programmatically modify width of elements in Grid, When in XAML it is set with *
我用這樣的分隔符定義了兩列:
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="6"/>
<ColumnDefinition Width="2*"/>
</Grid.ColumnDefinitions>
<ScrollViewer Name="sideBar" Margin="{StaticResource SplitLeft}" SizeChanged="ScrollViewer_SizeChanged">
<StackPanel>
<TextBlock Text="LEFT CONTENT" Style="{StaticResource Heading2}" />
<TextBlock Text="" Name="ShowThings"/>
</StackPanel>
</ScrollViewer>
<GridSplitter Grid.Column="1" />
<ScrollViewer Name="ListPage" Grid.Column="2 " Margin="{StaticResource SplitRight}" SizeChanged="ScrollViewer_SizeChanged">
<StackPanel>
<TextBlock Text="RIGHT CONTENT" Style="{StaticResource Heading2}" />
<TextBlock Text="Content goes here" />
</StackPanel>
</ScrollViewer>
所以頁面按 1:2 的比例划分。 但是如果用戶想調整它的大小,他可以。 我想在應用設置中保留修改后的寬度以供下次啟動時使用。
在尺寸更改事件中,我將新寬度存儲在設置中,
Properties.Settings.Default[((ScrollViewer)sender).Name + "Width"] = e.NewSize.Width;
Properties.Settings.Default.Save();
並在窗口構造函數中加載設置並設置值。
sideBar.Width = (double)Properties.Settings.Default["sideBarWidth"];
但這不起作用。 誰能幫我找出問題所在? 我確定這是一個重復的問題,但我不知道要搜索什么。
如果sideBar
的Width
取決於Grid.ColumnDefinitions
給定的大小,則設置它的Width
沒有意義。 嘗試設置ColumnDefinition.Width
:
// 0 = first column
grid.ColumnDefinitions[0].Width = new GridLength((double)Properties.Settings.Default["sideBarWidth"]);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.