在
CollectionView
中,我显示对象列表。每个显示屏旁边都有一个复选框。
理想情况下,当用户选中或取消选中复选框时,我会进入一个可以对对象执行操作的方法:以上是幻想代码。
实际代码
CheckedChangedEventArgs
传递到该方法中。我需要将该对象
ChcekedChangedEventArgs
转换为布尔值。 (请注意,我使用的是
MvvM
模式,因此我不使用代码隐藏。)
public class CheckedChangedArgsConverter : BaseConverterOneWay<CheckedChangedEventArgs, bool>
{
public override bool DefaultConvertReturnValue { get; set; } = false;
public override bool ConvertFrom(CheckedChangedEventArgs value, CultureInfo culture)
{
return value.Value;
}
}
但是,我不知道如何使用转换器。看来我这里有两个选择:事实是,我也不知道该怎么做
。有人可以帮忙吗? ====================================================== ===================
编辑:传递给 C# 方法的对象
CheckedChangedEventArgs
始终为 null。
这是我的Checkbox
代码。我使用
EventToCommandBehavior
因为这是我能想到的从复选框内部调用命令的唯一方法:<CheckBox x:Name="checkBox"
ScaleX="1.5"
ScaleY="1.5"
Color="#000063"
HorizontalOptions="End">
<CheckBox.Behaviors>
<toolkit:EventToCommandBehavior EventName="CheckedChanged"
Command="{Binding Source={x:Reference this},
Path=BindingContext.AddRemoveFromListCommand}"/>
</CheckBox.Behaviors>
</CheckBox>
我将
x:TypeArguments
添加到复选框中:
<CheckBox x:Name="checkBox"
ScaleX="1.5"
ScaleY="1.5"
Color="#000063"
HorizontalOptions="End">
<CheckBox.Behaviors>
<toolkit:EventToCommandBehavior
x:TypeArguments="CheckedChangedEventArgs"
EventName="CheckedChanged"
Command="{Binding Source={x:Reference this}, Path=BindingContext.AddRemoveFromListCommand}" />
</CheckBox.Behaviors>
</CheckBox>
这就成功了。
CommandParameter
来实现此目的。
您可以参考我的演示中的以下代码:<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:viewdels="clr-namespace:MauiCheckListviewApp.ViewModes"
xmlns:toolkit="http://schemas.microsoft.com/dotnet/2022/maui/toolkit"
x:Class="MauiCheckListviewApp.MainPage">
<ContentPage.BindingContext>
<viewdels:MyViewModel></viewdels:MyViewModel>
</ContentPage.BindingContext>
<VerticalStackLayout>
<CollectionView ItemsSource="{Binding Sections}" x:Name="myCollectionView"
Grid.Row="1">
<CollectionView.ItemTemplate>
<DataTemplate>
<HorizontalStackLayout>
<CheckBox Color="#0B4C90" IsChecked="{Binding IsChecked}">
<CheckBox.Behaviors>
<toolkit:EventToCommandBehavior
Command="{Binding BindingContext.UpdateThisItemCommand, Source={x:Reference myCollectionView}}"
CommandParameter="{Binding .}"
EventName="CheckedChanged" />
</CheckBox.Behaviors>
</CheckBox>
<Label Text="{Binding SectionName}"
FontSize="20"
FontAttributes="Bold"
TextColor="#0B4C90"/>
</HorizontalStackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
<Button Text="Get Sections" Command="{Binding GetResultCommand}"/>
</VerticalStackLayout>
</ContentPage>
public class MyViewModel: INotifyPropertyChanged
{
public ObservableCollection<Item> Sections { get; set; } = new ObservableCollection<Item>();
private Item _selectedModel;
public Item SelectedModel
{
get { return _selectedModel; }
set
{
SetProperty(ref _selectedModel, value);
if (SelectedModel != null)
{
System.Diagnostics.Debug.WriteLine("----> the selected Item " + SelectedModel.SectionName);
Shell.Current.GoToAsync(nameof(DetailPage));
SelectedModel = null;
}
}
}
public ICommand GetResultCommand { get; set; }
public ICommand UpdateThisItemCommand { get; set; }
public MyViewModel() {
Sections.Add(new Item { SectionName= "section_1"});
Sections.Add(new Item { SectionName= "section_2",IsChecked= true});
Sections.Add(new Item { SectionName= "section_3" });
GetResultCommand = new Command(getResult);
UpdateThisItemCommand = new Command<Item>(checkboxcommand);
}
private void checkboxcommand(Item obj)
{
if (obj != null)
{
if (obj.IsChecked)
{
// you also need to add some other logic codes here
System.Diagnostics.Debug.WriteLine($"{obj.SectionName}");
}
else {
System.Diagnostics.Debug.WriteLine($"{obj.SectionName}");
}
}
}
private void getResult()
{
foreach (var section in Sections)
{
if (section.IsChecked) {
System.Diagnostics.Debug.WriteLine(section.SectionName);
}
}
}
bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
{
if (Object.Equals(storage, value))
return false;
storage = value;
OnPropertyChanged(propertyName);
return true;
}
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
}
public class Item: INotifyPropertyChanged
{
public string SectionName { get; set; }
//public double Price { get; set; }
private bool _isChecked;
public bool IsChecked
{
set { SetProperty(ref _isChecked, value); }
get { return _isChecked; }
}
bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
{
if (Object.Equals(storage, value))
return false;
storage = value;
OnPropertyChanged(propertyName);
return true;
}
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
public event PropertyChangedEventHandler PropertyChanged;
}
您还可以向 Item 模型添加一个 bool 变量(例如
public bool IsChecked
)并将其绑定到此
CheckBox
的属性: <CheckBox Color="#0B4C90" IsChecked="{Binding IsChecked}">
一旦我们选中或取消选中
CheckBox
,它的值就会更新。