具有可绑定属性的自定义视图在Xamarin.Forms SAP上未正确绑定 [英] Custom View with Bindable Property not binding properly on Xamarin.Forms SAP
问题描述
我有一个复选框,该复选框应该触发按钮的IsEnabled事件.但是不知何故,原本应该执行的命令永远不会被正确绑定并因此而执行.
I have a checkbox that's supposed to fire the IsEnabled event of a button. But somehow the command that's supposed to do that never gets properly binded and therefore executed.
这是CheckBox.xaml.cs(控件)中的可绑定属性:
This is the bindable property in CheckBox.xaml.cs (the control):
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上所拥有的:
And this is what I have on the 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方法永远不会执行,因此我要设置的button属性永远不会更改.
The CheckBoxTapped method never gets executed and therefore the button property I wanna set never changes.
在此先感谢大家!
推荐答案
使用Xamarin.Forms.Behavior解决了该问题.它允许对控件进行多次绑定.
Solved it using Xamarin.Forms.Behavior. It allows multiple bindings to controls.
例如>
<Entry Placeholder="Enter password"
Text= "{Binding TxtFirstPasswordInput}"
IsPassword="true">
<b:Interaction.Behaviors>
<b:BehaviorCollection>
<b:EventToCommand EventName="Unfocused" Command="{Binding entry_Finished}"/>
<b:EventToCommand EventName="Focused" Command="{Binding entry_Focused}"/>
</b:BehaviorCollection>
</b:Interaction.Behaviors>
</Entry>
在ViewModel上
And on the ViewModel>
public PasswordInputViewModel()
{
entry_Finished = new Command(validateAndContinue);//unfocused
entry_Focused = new Command(entryFocused); //focused
}
这篇关于具有可绑定属性的自定义视图在Xamarin.Forms SAP上未正确绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!