数据绑定WPF组合框与“新建...”项 [英] Databound WPF ComboBox with 'New...' item

查看:174
本文介绍了数据绑定WPF组合框与“新建...”项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好结果
我有一个数据绑定到元素列表的组合框。但是除了元素列表,我想有另一个项目。 B中的想法是,如果他们选择正常的元素之一,它执行涉及该元素某些操作此项目应显示文本新建...结果$ B $。如果他们选择新建的元素,它会带他们到一个屏幕,就可以创建一个新的项目。结果
的问题是,当你的东西进行数据绑定您没得到另一个项目添加到它的选项,并没有添加一个虚拟项目元素...

Hi all
I have a combobox which is databound to a list of elements. But in addition to that list of elements, i want to have another item. This item should display the text 'New...'
The idea is if they select one of the normal elements, it performs some action involving that element. If they select the 'New' element, it will take them to a screen where they can create a new item.
The problem is, When you databind something you dont get the option to add another item to it, and there is no question of adding a dummy item to the list of elements...

名单的问题是建立在此基础上有一个组合框一个新的控制契机DefaultElement'属性? (与所有相关的模板,并命令绑定等)

Is this an opportunity to create a new control based on the ComboBox which has a 'DefaultElement' property? (with all of the associated templating and command binding etc)

推荐答案

要做到这一点我以前创建一个虚拟的包装类的普通型,让您绑定到主要包含正确的价值观,也是你的清单新建...之一,例如:

To do this I have previously created a dummy wrapper class for the normal type, allowing you to bind to a list containing mostly the correct values and also your "New..." one, e.g.

public class DisplayClass
{
    public DisplayClass(ModelClass mc)
    {
         this.mc = mc;
    }

    public string Name
    {
        get { return this.mc != null ? this.mc.Name : "New..."; }
    }

    public bool IsDummy
    {
        return this.mc == null;
    }

    public ModelClass Model
    {
        return this.mc;
    }
}

您可以再举办这些集合中的数据上下文(视图模型),并办理相应的选择基于 IsDummy 。这不是与此功能内置在控制自动的,但很简单,大概可以很容易地进行通用等可重复使用的。

You can then host a collection of these in your data context (ViewModel), and handle the selection appropriately based on IsDummy. It's not as automatic as a control with this functionality built in, but is pretty simple and could probably easily be made generic and so reusable.

这篇关于数据绑定WPF组合框与“新建...”项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆