Silverlight 中的点击编辑 [英] Click-to-edit in Silverlight
问题描述
有没有办法在 Silverlight 中制作点击编辑"控件?我有一些物品将显示在树视图控件中,我希望标签可以直接在树视图中编辑.
Is there a way to make a "click-to-edit" control in silverlight? I've got some items that will be displayed in a treeview control, and I would like the labels to be editable directly in the treeview.
有人知道怎么做吗?
推荐答案
其实很容易.我已经用这种交换机制实现了很多表单.
Very easy actually. I have implemented many forms with such a swapping mechanism.
您可以使用 Converter 执行此操作,并对绑定到 TreeView 的实体上存在的 IsEditable 属性进行简单的 BooleanToVisibility 转换.在您的 TreeView ItemTemplate 中,只需以这样一种方式绑定 TextBlock,即只要 IsEditable 属性为真,它就会折叠,并以这样一种方式绑定 TextBox,即当 IsEditable 属性为假时它会折叠(反之亦然).
You could do this using a Converter and do a simple BooleanToVisibility conversion on an IsEditable property that exists on the entities that you bind to your TreeView. Within your TreeView ItemTemplate just bind the TextBlock in such a way that it is Collapsed whenever the IsEditable property is true and bind the TextBox in such a way that it is collapesed when IsEditable property is false (and vice versa).
如果您想构建自定义 ClickToEdit 控件,您需要执行以下操作:
If you wanted to build a custom ClickToEdit control you would need to do the following:
- 创建一个继承自 ContentControl 的类
- 公开一个新的 DataTemplate 类型的依赖属性:一个名为 EditableTemplate 的属性.
- 在 OnApplyTemplate 中添加一个 MouseLeftButtonUp 事件处理程序来监听点击.
- 将活动内容模板更改为点击事件上的 EditableTemplate.
- 当控件失去焦点时将模板改回.
现在在 TreeView 中使用您的自定义控件:
Now to use your custom control inside TreeView:
- 为 TreeView 覆盖 ItemTemplate
- 将您的自定义 ClickToEdit 控件放入其中
实现自定义控件将允许您(或其他开发人员)轻松指定他们想要用作内容编辑器的控件.例如,他们可以指定 NumericUpDown 或 DateTimePicker 而不是仅使用 TextBox.
Implementing a custom control would allow you (or other developers) to easily specify what control they wanted to use as the content editor. For example, they could specify a NumericUpDown or a DateTimePicker instead of just using a TextBox.
查看 Silverlight 3 中的 DataForm.它具有类似的功能,但可编辑与只读的切换不是通过单击完成的.
Check out DataForm in Silverlight 3. It has similar functionality but the switching of the editable vs. read-only is not done by a click.
这篇关于Silverlight 中的点击编辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!