Caliburn.Micro,当键入不存在的项目时,SelectedItem 不会在 EditableCombobox 上触发
Caliburn.Micro, SelectedItem does not trigger on EditableCombobox when Non-Existing item is being typed
我正在尝试设置一个 CanExecute 方法,只要用户在具有绑定项目列表的 ComboBox 中键入信息。 I can disnable the button with CanAddCustomer (name is AddCustomer) without issues when there is an existing item selected;当输入为空(selectedCustomer 为空)时,禁用该按钮同样有效;但是这些事件似乎并没有在输入框中免费输入
- 已禁用因为为空,
- 已禁用,因为现有客户
- 应该启用,因为条目不存在
组合框的 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
的值
ComboBox
控件在您键入 ItemsSource
中不存在的值时将其 SelectedItem
属性 设置为 null
。
当 SelectedItem
为空且 ComboBox
为空时,您已经实现了视图模型以确实启用 ComboBox
。
在这种情况下,也许您最好绑定到 ComboBox
的 Text
属性。它应该 return 当前在 TextBox
.
中的任何内容
SelectedItem
属性 只能设置为 ItemsSource
.
中的值
您需要在ComboBox中设置Text的属性,然后绑定到您的属性。
<ComboBox
x:Name="Customers" Height="40"
Text="{Binding Customer, Mode=TwoWay}"
DisplayMemberPath="CustomerName"
Controls:TextBoxHelper.UseFloatingWatermark="False"
Controls:TextBoxHelper.Watermark="Select a Customer"
IsEditable="True"
MaxDropDownHeight="125"
Margin="0 0 15 0"
Grid.Column="0"
/>
我正在尝试设置一个 CanExecute 方法,只要用户在具有绑定项目列表的 ComboBox 中键入信息。 I can disnable the button with CanAddCustomer (name is AddCustomer) without issues when there is an existing item selected;当输入为空(selectedCustomer 为空)时,禁用该按钮同样有效;但是这些事件似乎并没有在输入框中免费输入
- 已禁用因为为空,
- 已禁用,因为现有客户
- 应该启用,因为条目不存在
组合框的 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
ComboBox
控件在您键入 ItemsSource
中不存在的值时将其 SelectedItem
属性 设置为 null
。
当 SelectedItem
为空且 ComboBox
为空时,您已经实现了视图模型以确实启用 ComboBox
。
在这种情况下,也许您最好绑定到 ComboBox
的 Text
属性。它应该 return 当前在 TextBox
.
SelectedItem
属性 只能设置为 ItemsSource
.
您需要在ComboBox中设置Text的属性,然后绑定到您的属性。
<ComboBox
x:Name="Customers" Height="40"
Text="{Binding Customer, Mode=TwoWay}"
DisplayMemberPath="CustomerName"
Controls:TextBoxHelper.UseFloatingWatermark="False"
Controls:TextBoxHelper.Watermark="Select a Customer"
IsEditable="True"
MaxDropDownHeight="125"
Margin="0 0 15 0"
Grid.Column="0"
/>