Wpf 可编辑组合框

Wpf 可编辑组合框,wpf,combobox,wpf-controls,Wpf,Combobox,Wpf Controls,我想创建具有以下属性的可编辑组合框: 将文本属性绑定到我的数据模型。 数据模型可能会覆盖GUI中的更改,即使选择已更改。例如,我可以从1、2、3中选择,我选择2,但下面的某个组件将其更改为3。 在以下事件中更新数据模型: 选择改变了 失去焦点 按下Enter键应与失去焦点的行为相同。 我已经能够创建这样的控制,但它是相当丑陋的使用许多黑客,我希望有一个更简单的方法 提前谢谢好的,我这里是我所做的,并没有那么难看: /// <summary> /// Editable combo b

我想创建具有以下属性的可编辑组合框:

将文本属性绑定到我的数据模型。 数据模型可能会覆盖GUI中的更改,即使选择已更改。例如,我可以从1、2、3中选择,我选择2,但下面的某个组件将其更改为3。 在以下事件中更新数据模型:

选择改变了 失去焦点 按下Enter键应与失去焦点的行为相同。 我已经能够创建这样的控制,但它是相当丑陋的使用许多黑客,我希望有一个更简单的方法


提前谢谢

好的,我这里是我所做的,并没有那么难看:

 /// <summary>
/// Editable combo box which updates the data model on the following:
/// 1. Select## Heading ##ion changed
/// 2. Lost focus
/// 3. Enter or Return pressed
/// 
/// In order for this to work, the EditableComboBox requires the follows, when binding:
/// The data model value should be bounded to the Text property of the ComboBox
/// The binding expression UpdateSourceTrigger property should be set to LostFocus
/// e.g. in XAML:
/// <PmsEditableComboBox Text="{Binding Path=MyValue, UpdateSourceTrigger=LostFocus}"
/// ItemsSource="{Binding Path=MyMenu}"/>
/// </summary>
public class PmsEditableComboBox : ComboBox
{
    /// <summary>
    /// Initializes a new instance of the <see cref="PmsEditableComboBox"/> class.
    /// </summary>
    public PmsEditableComboBox()
        : base()
    {
        // When TextSearch enabled we'll get some unwanted behaviour when typing
        // (i.e. the content is taken from the DropDown instead from the text)
        IsTextSearchEnabled = false;
        IsEditable = true;
    }

    /// <summary>
    /// Use KeyUp and not KeyDown because when the DropDown is opened and Enter is pressed
    /// We'll get only KeyUp event
    /// </summary>
    protected override void OnKeyUp(KeyEventArgs e)
    {
        base.OnKeyUp(e);

        // Update binding source on Enter
        if (e.Key == Key.Return || e.Key == Key.Enter)
        {
            UpdateDataSource();
        }
    }

    /// <summary>
    /// The Text property binding will be updated when selection changes
    /// </summary>
    protected override void OnSelectionChanged(SelectionChangedEventArgs e)
    {
        base.OnSelectionChanged(e);
        UpdateDataSource();
    }

    /// <summary>
    /// Updates the data source.
    /// </summary>
    private void UpdateDataSource()
    {
        BindingExpression expression = GetBindingExpression(ComboBox.TextProperty);
        if (expression != null)
        {
            expression.UpdateSource();
        }
    }

}

最简单的方法是对绑定使用UpdateSourceTrigger属性。您可能无法精确匹配您当前的行为,但您可能会发现它具有可比性

UpdateSourceTrigger属性控制绑定的目标何时更新源。绑定时,不同的WPF控件对此属性具有不同的默认值

以下是您的选择:

UpdateSourceTrigger.Default=允许目标控件确定UpdateSourceTrigger模式

UpdateSourceTrigger.Explicit=只有在有人调用BindingExpression.UpdateSource时才更新源

UpdateSourceTrigger.LostFocus=每当目标失去焦点时自动更新绑定源。这样可以完成更改,然后在用户继续移动后更新绑定

UpdateSourceTrigger.PropertyChanged=只要目标上的DependencyProperty值发生更改,源就会立即更新。大多数UserControls不默认使用此属性,因为它需要更多绑定更新,这可能是一个性能问题