【发布时间】:2023-04-08 17:03:01
【问题描述】:
我有一个复选框应该触发按钮的 IsEnabled 事件。但是不知何故,应该执行的命令永远不会正确绑定并因此执行。
这是 CheckBox.xaml.cs(控件)中的可绑定属性:
public static readonly BindableProperty CheckBoxCommandProperty =
BindableProperty.Create<CheckBox, ICommand>(
checkbox =>
checkbox.CheckBoxCommand,
null,
propertyChanged: (bindable, oldValue, newValue) =>
{
CheckBox checkbox = (CheckBox)bindable;
EventHandler<bool> eventHandler = checkbox.CheckedChanged;
if (eventHandler != null)
{
eventHandler(checkbox, checkbox.IsChecked);
}
});
public event EventHandler<bool> CheckedChanged;
public ICommand CheckBoxCommand
{
get { return (ICommand)GetValue(CheckBoxCommandProperty); }
set { SetValue(CheckBoxCommandProperty, value); }
}
这就是我在 ViewModel 上的内容:
public ICommand Next_OnClick { get; set; }
public ICommand OnCheckBoxTapChanged { get; set; }
public TermsAndConditionsViewModel()
{
Next_OnClick = new Command(NextClicked);
OnCheckBoxTapChanged = new Command(CheckBoxTapped);
}
private void CheckBoxTapped()
{
if (IsCheckedChanged)
{
Next_IsEnabled = true;
}
else
{
Next_IsEnabled = false;
}
}
CheckBoxTapped 方法永远不会被执行,因此我要设置的按钮属性永远不会改变。
提前谢谢各位!
【问题讨论】:
-
为什么不能同时绑定按钮可见性到某个 bool 属性,然后绑定到使用转换器检查的相同属性绑定复选框?
标签: c# mvvm xamarin xamarin.forms shared-project