WPF 组合框添加项目
在 Access 2010(以及其他?)中,如果将“列表项目编辑表单”分配给组合框,则打开组合框时会出现一个半透明的编辑图标,您可以单击该图标并打开一个表单来添加/编辑组合框数据源中的项目。有谁知道这是否可以在 WPF/Xaml 中复制,如果可以,如何复制?它看起来非常光滑,比组合框旁边的按钮要好得多,这就是我目前的做法(我发现了很多如何在组合框列表顶部添加项目的示例,但这不是我想要什么
非常感谢
凯夫·T
In Access 2010 (and others?) if you assign a 'List Items Edit Form' to a combo box, you get a semi transparent edit icon appear when you open the combo box, which you can click on and open a form to add/edit items in the combo box's data source. Does anyone know if this can be replicated in WPF/Xaml and if so how? It looks very slick and far better than a button next to the combo box which is how I do it at the moment ( I have found lots of examples of how to add an item at the top of the combo box list but that isn't what I'm after
Many Thanks
Kev T
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这不是一个完整的答案,但我有一些想法。我已经尝试过全面思考,但可能有些事情不太正确或可以做得更好。
创建一个新的类,继承自ComboBox;我们称之为 EditableComboBox。给它一个 Button 类型的 TemplatePart,其名称为 PART_EditButton。还向类型对象的 EditableComboBox 添加依赖属性;我们称之为 EditorContent。 (这将是弹出对话框的内容。)
重写 OnApplyTemplate 以获取对按钮的引用(使用 GetTemplateChild)并将事件处理程序附加到按钮的 Click 事件。当 Click 事件触发时,创建并显示一个子窗口,并将其 Content 属性设置为 EditorContent。将其 DataContext 属性设置为 EditableComboBox 的 DataContext。
使用 Blend 为 Button 创建默认 ControlTemplate 的副本;我们将其称为 MyButtonControlTemplate。将按钮的不透明度更改为小于 1 的值,并将 Storyboard 添加到将不透明度设置为 1 的 MouseOver VisualState。
使用 Blend 为 ComboBox 创建默认 ControlTemplate 的副本;我们将其称为 EditableComboBoxTemplate。看到弹出节点了吗?将另一个透明行添加到其子网格,并在该行中添加一个名为 PART_EditButton 的按钮。将按钮的模板设置为 MyButtonControlTemplate。然后为 EditableComboBox 创建默认样式,将模板设置为 EditableComboBoxTemplate。
使用 EditableComboBox 时,将其 EditorContent 属性设置为可以编辑项目的内容,可能是包含与 EditableComboBox 绑定到相同 ItemsSource 的 ListBox 的 Grid。
我希望这至少有一些帮助。我想提供示例代码,但不幸的是我现在没有足够的时间。这是一个有趣的问题,所以我会稍后再回来查看是否有人给了您更好的想法和/或回答您可能有的进一步问题。
I don't think this is a complete answer, but I have some ideas. I've tried to think it all through, but there are probably things that aren't quite right or could be done better.
Create a new class that inherits from ComboBox; let's call it EditableComboBox. Give it a TemplatePart of type Button whose name is PART_EditButton. Also add a dependency property to EditableComboBox of type object; let's call it EditorContent. (This will be the content of the popup dialog.)
Override OnApplyTemplate to get a reference to the button (using GetTemplateChild) and attach an event handler to the button's Click event. When the Click event fires, create and show a child Window, and set its Content property to EditorContent. Set its DataContext property to the DataContext of the EditableComboBox.
Use Blend to create a copy of the default ControlTemplate for Button; let's call it MyButtonControlTemplate. Change the button's Opacity to something less than 1, and add a Storyboard to the MouseOver VisualState that sets Opacity to 1.
Use Blend to create a copy of the default ControlTemplate for ComboBox; let's call it EditableComboBoxTemplate. See the Popup node? Add another transparent row to its child Grid, and add a button in that row named PART_EditButton. Set the button's Template to MyButtonControlTemplate. Then create a default style for EditableComboBox that sets the Template to EditableComboBoxTemplate.
When using EditableComboBox, set its EditorContent property to something that can edit the items, perhaps a Grid containing a ListBox bound to the same ItemsSource as the EditableComboBox.
I hope that's at least somewhat helpful. I'd like to provide sample code, but unfortunately I don't have enough time at the moment. This is an interesting question, so I'll try to check back later to see if someone else has given you a better idea and/or answer further questions you might have.