Xamarin标签单击更改复选框仅状态Xaml
我正在尝试更改Xamairn中复选框的状态 我的Xaml代码Xamarin标签单击更改复选框仅状态Xaml,xaml,xamarin,xamarin.forms,Xaml,Xamarin,Xamarin.forms,我正在尝试更改Xamairn中复选框的状态 我的Xaml代码 <StackLayout Orientation="Horizontal"> <CheckBox VerticalOptions="Start" IsChecked="{Binding IsChecked,Mode=TwoWay}"> <CheckBox.Margin> <OnPlatform x:TypeArguments="T
<StackLayout Orientation="Horizontal">
<CheckBox VerticalOptions="Start"
IsChecked="{Binding IsChecked,Mode=TwoWay}">
<CheckBox.Margin>
<OnPlatform x:TypeArguments="Thickness">
<On Platform="iOS" Value="0,-10,0,0" />
<On Platform="Android, WinPhone, Windows" Value="0,-5,0,0" />
</OnPlatform>
</CheckBox.Margin>
</CheckBox>
<Label VerticalTextAlignment="Center" VerticalOptions="Start"
Text="{Binding Text}">
<Label.GestureRecognizers>
<TapGestureRecognizer>
</TapGestureRecognizer>
</Label.GestureRecognizers>
</Label>
</StackLayout>
我知道我以前做过,但我忘了
我必须在TapGestureRecognitor中添加一个setter或其他内容,以使复选框IsChecked=True
而IsChecked=False,当其为True时要以MVVM方式执行此操作,您需要的是
命令
在ViewModel中添加以下命令:
public ICommand ChangeStateCommand { get; set; }
在构造函数中初始化它:
ChangeStateCommand = new Command(() => IsChecked = !IsChecked);
然后,您需要使用标签
的标签标签标签
将其绑定
<StackLayout Orientation="Horizontal" VerticalOptions="CenterAndExpand">
<CheckBox VerticalOptions="Start"
IsChecked="{Binding IsChecked,Mode=TwoWay}">
<CheckBox.Margin>
<OnPlatform x:TypeArguments="Thickness">
<On Platform="iOS" Value="0,-10,0,0" />
<On Platform="Android, WinPhone, Windows" Value="0,-5,0,0" />
</OnPlatform>
</CheckBox.Margin>
</CheckBox>
<Label VerticalTextAlignment="Center" VerticalOptions="Start"
Text="{Binding Text}" >
<Label.GestureRecognizers>
<TapGestureRecognizer Command="{Binding ChangeStateCommand}" />
</Label.GestureRecognizers>
</Label>
</StackLayout>
希望这有帮助。-为复选框指定一个名称,为标签指定一个TapeEvent,然后在代码隐藏中更改isChecked属性:
<StackLayout Orientation="Horizontal">
<CheckBox VerticalOptions="Start" x:Name="myCheckBox"
IsChecked="{Binding IsChecked,Mode=TwoWay}">
<CheckBox.Margin>
<OnPlatform x:TypeArguments="Thickness">
<On Platform="iOS" Value="0,-10,0,0" />
<On Platform="Android, WinPhone, Windows" Value="0,-5,0,0" />
</OnPlatform>
</CheckBox.Margin>
</CheckBox>
<Label VerticalTextAlignment="Center" VerticalOptions="Start"
Text="{Binding Text}">
<Label.GestureRecognizers>
<TapGestureRecognizer Tapped="TapGestureRecognizer_Tapped">
</TapGestureRecognizer>
</Label.GestureRecognizers>
</Label>
</StackLayout>
private void TapGestureRecognizer_Tapped(object sender, EventArgs e)
{
myCheckBox.IsChecked = !myCheckBox.IsChecked;
//or change the IsChecked property in ViewModel
//myViewModel.IsChecked = !myViewModel.IsChecked;
}
现在想象一下,如果我现在在一个列表中有10个,那将是非常糟糕的。您在问题中没有提到列表,如果是这样,一个命令可能会起作用,并更改模型中相应的IsChecked属性。