如何获取在运行时更改的 WPF 面板的大小?
我有一个 Panel
,其 Width
可以在运行时调整大小:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="770*"/>
</Grid.ColumnDefinitions>
<panels:NavigationPanel x:Name="cmBar" Margin="2,2,0,2"
HorizontalAlignment="Left" Width="220"/>
<panels:DetailAreaPanel x:Name="detailGrid" Margin="224,2,2,2" />
</Grid>
当程序关闭时,我想将新的 Width
保存在注册表中。因此,程序下次打开时将加载到相同的大小。除了 Width
之外,我的所有功能都正常,除非我对新的 Width
进行硬编码。所以我会假设我的保存是错误的。
all[5] = cmBar.ActualWidth.ToString();
然后 all[]
被写入注册表。无论面板大小如何调整 cmBar.ActualWidth 始终为 220。有什么想法吗?
I have a Panel
whose Width
can be resized during runtime:
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="770*"/>
</Grid.ColumnDefinitions>
<panels:NavigationPanel x:Name="cmBar" Margin="2,2,0,2"
HorizontalAlignment="Left" Width="220"/>
<panels:DetailAreaPanel x:Name="detailGrid" Margin="224,2,2,2" />
</Grid>
When the program is closed, I want to save the new Width
in the registry. So the program will load to the same size next time its opened. I have everything working except the Width
, unless I hardcode the new Width
. So I would assume that my save is wrong.
all[5] = cmBar.ActualWidth.ToString();
all[]
is then wrote into the registry. No matter how the panel is resized cmBar.ActualWidth is always 220. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
HorizontalAlignment="Stretch" 将允许控件调整其父级的大小。 “左”仅调整其自身大小,并在其右侧留下空白空间。
HorizontalAlignment="Stretch" will allow the control to size to its parent. "Left" just sizes to itself and leaves empty space to its right.
Aviad P. 有一个正确的
.Width
是导致问题的原因。解决方案是当我加载宽度时将其加载为.MaxWidth
这将调整大小,但允许.ActualWidth
具有面板的真实宽度。Aviad P. had one this right the
.Width
is what is causing the problem. The solution is is when i load the width to load it as.MaxWidth
This will do the resizing but allow.ActualWidth
to have the real width of the panel.Width="220"
的小片段将宽度设置为 220,除非从代码隐藏中显式更改,否则它不会改变。您是否在代码隐藏中更改面板的宽度?如果不是 - 那么它的宽度实际上不会改变,并且在其整个生命周期中保持在 220。The little piece that says
Width="220"
sets the width to 220, and that will not change unless explicitly changed from the code-behind. Are you changing the panel's width in code-behind? If not - then its width really doesn't change, and stays at 220 throughout its lifetime.