点击到编辑在Silverlight [英] Click-to-edit in Silverlight
问题描述
有没有一种方法,使Silverlight中的一个点击编辑的控制?我有一些项目是
将显示在一个TreeView控件,我想的标签可编辑直接在树视图。
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.
任何人都知道如何做到这一点?
Anyone know how to do this?
推荐答案
很容易实际。我已经实现了这样的交换机制多种形式。
Very easy actually. I have implemented many forms with such a swapping mechanism.
您可以做到这一点使用一个转换器和做对即在实体上存在的IsEditable产权简单BooleanToVisibility转换你绑定到你的TreeView。在您的TreeView的ItemTemplate只是绑定文本块以这样一种方式,它是折叠每当IsEditable属性为true,并绑定文本框以这样的方式,它是collapesed当IsEditable属性为false(反之亦然)。
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。
- 更改模板回当控件失去焦点
- Create a class that inherits from ContentControl
- Expose a new dependency properties of type DataTemplate: one called EditableTemplate.
- Add a MouseLeftButtonUp event handler inside your OnApplyTemplate to listen for the click.
- Change the active content template to be your EditableTemplate on the click event.
- Change the template back when the control loses focus.
现在使用里面的TreeView自定义控件:
Now to use your custom control inside TreeView:
- 覆盖您的ItemTemplate为您的TreeView
- 把你的自定义ClickToEdit控制里边有
实现自定义控制将允许你(或其他开发者)可以轻松地指定他们想要的内容编辑器使用什么样的控制。例如,他们可以指定一个或的NumericUpDown一个DateTimePicker,而不是只使用一个文本框的。
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.
检查出来的DataForm在Silverlight 3,具有类似的功能,但可编辑的开关与只读不被点击进行。
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屋!