如何在代码中设置固定的列/行定义并在 DesignView 中使用它们
我想要具有固定数量的列和行的网格。例如 80 列和 24 行。 在设计视图中,用户不能更改列和行定义,但必须能够将控件插入单元格中。
设计视图应类似于 此 http://imageshack.us/f/29/coldefs.png< /a>
我尝试子类化 Grid 并在构造函数内设置列和行定义,但单元格根本没有显示在 DesignView 中。
这可以做到吗?
I'd like to have Grids with fixed amount of column and rows. 80 columns and 24 rows for example.
In design view, the user must not be able to change the column and row definitions but he must be able to insert controls into the cells.
Design View should look like this http://imageshack.us/f/29/coldefs.png
I tried to subclass Grid and set the Column and Row Definitions inside the constructor, but then the cells did not show up in DesignView at all.
Can this be done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以子类化
Grid
以添加依赖属性Rows
和Columns
,并在PropertyChanged回调中更新RowDefinitions
和相应地ColumnDefinitions
。要在设计时获取 GridLines,如果处于设计模式,您可以在
Loaded
事件中向每个单元格添加Border
(DesignerProperties.GetIsInDesignMode(this) == true
)像这样使用它
,设计器中的输出将如下所示。在运行时,您将不会得到任何
边框
。编辑
添加和删除元素时,Children 属性(即
Grid
的ContentProperty
)不断在框架中重置。这意味着网格边界也会被清除,据我所知,没有办法通知这一点。为了解决这个问题,我必须将FixedSizeGrid
的ContentProperty
更改为新的 DP,从而更新 Children 和 DesignMode Grid-Borders。可以进行多种优化,例如每次都清除和读取网格边框。无论如何,这是更新后的代码。它现在应该可以在设计器中工作,如果设置了许多行/列,可能会有点慢,但这可能可以通过一些优化来修复。
固定大小网格
You could subclass
Grid
to add the dependency propertiesRows
andColumns
and in the PropertyChanged callback you update theRowDefinitions
andColumnDefinitions
accordingly.To get GridLines in design time you can add a
Border
to each cell in theLoaded
event if you are in Design Mode (DesignerProperties.GetIsInDesignMode(this) == true
)Use it like this
And the output in the designer will look like below. In runtime you'll get no
Borders
.Edit
The Children Property (which is the
ContentProperty
forGrid
) keeps getting reset in the framework when adding and removing elements. This means that the Grid-Borders get wiped clean as well and there is no way to be notified about this as far as I can tell. To work around this I had to change theContentProperty
ofFixedSizeGrid
to a new DP which in turn updates both Children and the DesignMode Grid-Borders.There are several optimizations that can be made, clearing and readding the Grid-Borders everytime for example. Anyway, here is the updated code. It should work in the designer now, perhaps a little slow if many rows/columns are set but this can probably be fixed with some optimizations.
FixedSizeGrid
如果您想查看单元格或方块,可以将 ShowGridLines 属性设置为 true
You can set
ShowGridLines
property to true if you want to see the cell or squares