了解 XAML Silverlight 模型:向画布添加线条?
我有一个画布元素,我想在上面画网格线(所以它最终看起来像工程纸)。
在我看来,有几种方法可以做到这一点:
- 使用 XAML 并将“Path”元素复制/粘贴到画布中,并改变顶部、左侧属性
- 使用代码添加网格线
- 数据绑定?
XAML 很简单,但很麻烦。 代码也很简单。
我不确定最后一个。我一直认为使用代码隐藏来构建控件是 Winform 的做事方式,而且我更喜欢以“正确的方式”做事。那么我会创建一个“顶部”和“左侧”值的列表,并从中生成行吗?或者数据绑定真的用于将数据放入 Excel 网格等中吗?
I have a canvas element and I'd like to draw grid lines on it ( so it ends up kind of looking like engineering paper),.
As I see it, there are a couple ways to do this:
- Use XAML and copy/paste "Path" elements into the canvas and vary the top,left properties
- Use Code to add grid lines
- Databinding?
XAML is straight forward, but cumbersome.
Code is also straight forward.
I'm not sure about the last one. I keep thinking using code-behind to build controls is the Winforms way of doing things, and I'd prefer to do things the "proper way." So would I create a list of "top" and "left" values, and have lines generated from that? Or is databinding really used for putting data in excel grids and the like?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一定要用代码。它将让您灵活地修改和调整网格间距,并且绝对遵循 DRY 原则。
您可以编写类似以下伪代码的内容:
如果您愿意,可以添加,您可以将所有线条放置在一个矩形内,以便您可以轻松地显示/隐藏矩形,从而显示/隐藏网格。
Definitely use code. It will give you the flexibility to modify and adjust this spacing of your grid and definitely follows DRY principles.
You could write something like the following pseudo-code:
Add if you like, you can place all lines inside a rectangle so that you can easily show/hide the rectangle thereby show/hide the grid.
如果这些行是静态的,即它们的位置不会改变,我会将它们添加到代码隐藏中以提高速度和简单性。
数据绑定适用于绑定动态(即变化)的数据。此外,通过将 XAML 中的数据外观定义为模板,您可以为 UI 进行“皮肤”或“主题化”。但是,就您的情况而言,我认为简单的方法可能是最好的。
If the lines are static, i.e. their locations do not change, I would add them in code-behind for speed and simplicity.
Databinding is good for binding data that is dynamic, i.e. changing. Also, defining the appearance of the data in XAML as a template allows you to 'skin' or 'theme' your UI. However, in your case I think a simple approach is probably the best.