添加“添加新内容”项目到组合框 [英] Add "Add new" item to ComboBox

查看:63
本文介绍了添加“添加新内容”项目到组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,这甚至对我来说都是微不足道的,但我无法找到答案。
我有一个普通的 ComboBox ,可以正常工作。

Ok, this sounds trivial even to me, but I'm not able to find an answer to this. I have a normal ComboBox, which works perfectly.

我想做的是添加元素 (作为<$的最后一项c $ c>下拉列表),不过只是添加新 标签;选择此项目后,我想打开一个窗口,该窗口使我可以定义要添加的元素的属性。

What I would like to do is add an element (as the last item of the drop-down list) which is nothing but a Add new label; when this item is selected, I want to open a window which allows me to define the properties of the element I'm adding.

现在我有以下内容:

<ComboBox x:Name="myCombo"
    SelectedValue="{Binding Name, UpdateSourceTrigger=PropertyChanged}"
    ItemsSource="{Binding Definitions}" 
    SelectedValuePath="Name"
    DisplayMemberPath="Name"
    SelectionChanged="CheckToAdd" />

CheckToAdd 函数如下所示:

private void CheckToAdd(object sender, SelectionChangedEventArgs e)
{
    if (((myViewModel)e.AddedItems[0]).Name.Value.Equals("Add new"))
    {
        AddNewItem(sender, null);
    }
}

我要做的是有一个 dummy 元素在我的定义中,其中添加新名称。使用这种方法可以正常工作,但是IMO 很难看(因为问题只是 view 之类的东西,所以我认为弄混 VM或模型

What I do is have a dummy element in my Definitions which has Add new as Name. Using this approach makes all working, but it's ugly IMO (because the problem is just a view thing, so I don't think it's correct to mess with the VM or the Model).

有人有更好的主意吗?

推荐答案

WPF没有ASP.NET的 AppendDataBoundItems 等效。您可以通过修改ControlTemplate到达某个地方,或者可以简化当前方法tp使其更具可读性。像这样的东西:

There's no equivalent of ASP.NET's AppendDataBoundItems for WPF. You may be able to get somewhere by modifying the ControlTemplate, or you could simplify your current approach tp make it more readable. Something like:

private void CheckToAdd(object sender, SelectionChangedEventArgs e) {
    if (cbo.SelectedIndex == Definitions.Count()) {
        AddNewItem(sender, null);
    }

这篇关于添加“添加新内容”项目到组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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