本文介绍了WPF 可编辑组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ComboBox 并且 ComboBox.IsEditable 属性设置为 True 以使 ComboBox 同时充当文本框和下拉列表.但是当 ComboBox 是数据绑定时,输入自定义文本不会导致新项目被添加到数据绑定集合中.

I have a ComboBox and ComboBox.IsEditable property is set to True to have a ComboBox act as both a TextBox and a drop-down list simultaneously. But when the ComboBox is data-bound, entering custom text will not cause a new item to be added to the data-bound collection.

例如,如果我在绑定到不包含值Joe"的人员列表的 ComboBox 中输入Joe",则不会将值Joe"添加到下拉列表中- 自动列出.

For example, if I enter 'Joe' in a ComboBox that is bound to a list of people, which doesn't contain the value 'Joe', then the value 'Joe' is not going to be added to the drop-down list automatically.

处理这个问题的最佳方法是什么?

What is the best way to handle this?

推荐答案

这是获得所需行为的基本 MVVM 兼容方式:

Here's a basic MVVM compliant way of getting the behaviour you want:

MainWindow.xaml

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <ComboBox Margin="30,5,30,5"
                  IsEditable="True"
                  ItemsSource="{Binding Items}"
                  SelectedItem="{Binding SelectedItem}"
                  Text="{Binding NewItem, UpdateSourceTrigger=LostFocus}"/>
        <TextBox Margin="30,5,30,5" />
    </StackPanel>
</Window>

MainWindow.cs

public partial class MainWindow : Window, INotifyPropertyChanged
{
    private string _selectedItem;

    private ObservableCollection<string> _items = new ObservableCollection<string>()
    {
        "One",
        "Two",
        "Three",
        "Four",
        "Five",
    };

    public MainWindow()
    {
        InitializeComponent();
        this.DataContext = this;
    }

    public IEnumerable Items
    {
        get { return _items; }
    }

    public string SelectedItem
    {
        get { return _selectedItem; }
        set
        {
            _selectedItem = value;
            OnPropertyChanged("SelectedItem");
        }
    }

    public string NewItem
    {
        set
        {
            if (SelectedItem != null)
            {
                return;
            }
            if (!string.IsNullOrEmpty(value))
            {
                _items.Add(value);
                SelectedItem = value;
            }
        }
    }

    protected void OnPropertyChanged(string propertyName)
    {
        var handler = this.PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
}

我必须在窗口中放置另一个控件,因为我已将 Text 绑定的 UpdateSourceTrigger 属性设置为 LostFocus.如果窗口中没有其他控件,则 Combobox 永远不会失去焦点.

I had to place another control in the window as I have set the UpdateSourceTrigger property of the Text binding to LostFocus. If there are no other controls in the window then the Combobox will never lose focus.

我更改了更新模式,因为默认更新模式是 Propertychanged,它会为每次击键添加一个新项目.

I changed the update mode because the default update mode is Propertychanged which will add a new item for each keystroke.

E.G.如果您输入文本Window",以下内容将添加到您的收藏中:

E.G. if you entered the text "Window", the following would be added to your collection:

W
Wi
Win
Wind
Windo
Window

这篇关于WPF 可编辑组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 18:48