Visual Studio 如何设置 WPF 控件的大小?
在Visual Studio中,当将控件从工具箱拖到设计区域时,VS会自动设置控件的宽度和高度一些值。你知道它是怎么做到的吗?我怀疑 Width 和 Height 属性甚至类本身存在某种属性,但无法从反映 Button 控件中找到任何内容。
In visual studio, when dragging a control from the toolbox onto the design area, VS automatic sets width and height of the control with some values. Do you know how it does it? I was suspicious that there were some kind of attributes for the Width and Height properties or even the class itself but could not find anything from reflecting the Button control.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上,VS 不需要做任何事情来获取这些默认值。我的猜测是,它允许所有布局过程完成,然后简单地复制 ActualWidth 和 ActualHeight 属性的值。
Google 了一下
MeasureOverride
和ArrangeOverride
方法、布局通道等的用途。请记住,依赖属性的实际默认值是由其元数据连同其注册一起指定的,或者通过在子类中覆盖它来指定。我相信,
Width
和Height
属性的实际默认值是double.NaN
值。Actually, there is no any magic, the VS would need to do to acquire those defaults. My guess, it allows all layout passes to finish and then simply copies values of
ActualWidth
andActualHeight
properties.Google a little on the purpose of
MeasureOverride
andArrangeOverride
methods, layout passes, etcetera.Have in mind, that actual default values for dependency properties are specified by their metadata either along with their registration or by means of overriding it in subclasses. I believe, that actual default value for either of
Width
andHeight
properties is thedouble.NaN
value.我还没有发现它使用反射器,至少在 Button 类中没有。当我使用 xaml 添加按钮时,它将占用为其提供的可用空间。当我使用设计器添加一个时,其高度设置为 23。
本文描述了创建一个具有默认值的控件以在工具箱中使用,我假设工具箱项目以相同的方式完成。
I haven't found it using reflector yet, at least not in the Button class. When i add a button using xaml, it will take up the available space given to it. when i add one using the designer, its height gets set at 23.
this article describes creating a control with default values for use in the toolbox, i would assume that the toolbox items are done the same way.