首页 > 解决方案 > Caliburn.Micro,当键入非现有项目时,SelectedItem 不会在 EditableCombobox 上触发

问题描述

一旦用户在带有绑定项目列表的 ComboBox 中键入信息,我就会尝试设置 CanExecute 方法。I can disnable the button with CanAddCustomer (name is AddCustomer) without issues when there is an existing item selected; 当输入为空(selectedCustomer 为空)时,禁用按钮也有效;但这些事件似乎并没有在输入框中免费输入

空的,已禁用

现有客户,已禁用

不存在的项目,应该启用

  1. 禁用因为空,
  2. 已禁用,因为现有客户
  3. 应该启用,因为不存在条目

组合框的 WPF

<ComboBox 
    x:Name="Customers" Height="40"
    DisplayMemberPath="CustomerName"
    Controls:TextBoxHelper.UseFloatingWatermark="False"
    Controls:TextBoxHelper.Watermark="Select a Customer"
    IsEditable="True"
    MaxDropDownHeight="125"
    Margin="0 0 15 0"
    Grid.Column="0" 
/>

ViewModel 中的数据:

private Customer _selectedCustomer ;
private string _customer;

public string Customer
{
    get { return _customer; }
    set {
        _customer = value;
        NotifyOfPropertyChange(() => CanAddCustomer);
    }
} 


public Customer SelectedCustomer 
{
    get { return _selectedCustomer; }
    set {
        _selectedCustomer = value;
        NotifyOfPropertyChange(() => CanAddCustomer);
        GetAllOrdersPerCustomer();
    }
}


public bool CanAddCustomer
{
    get
    {
        Customer cs = SelectedCustomer;
        if (cs == null && string.IsNullOrEmpty(Customer))
        {
            return false;
        } else if (cs != null ) {
            return false;
        } else
        {
            return true;
        }
    }

} 

public void AddCustomer()
{
    Debug.WriteLine("You cliked");
}

我知道这个问题,当我调试项目时,我可以看到“SelectedCustomer”没有被触发......但是......然后我如何引用文本框中的文本?该public string Customer属性是我的尝试,但这只是 selectedItem 的值

标签: c#wpfcaliburn.micro

解决方案


当您键入一个不存在ComboBox于其.SelectedItemnullItemsSource

您已经实现了您的视图模型,以确实启用ComboBoxwhenSelectedItem为 null 并且 theComboBox为空。

在这种情况下,也许你最好绑定到的Text属性。ComboBox它应该返回当前在TextBox.

SelectedItem属性只能设置为ItemsSource.


推荐阅读