English 中文(简体)
如何通过多个参数
原标题:Maui EventToCommandBehavior how to pass multiple parameters

What I m trying to accomplish

页: 1 结 论 我也需要通过正常的旧参数。

“entergraph

它可能只是一个yn子问题,我也认为是正确的。 我希望这样做。

My current code

确实很简单。 指挥在<条码>中进行。 View。 基本上,我有一个“Video”类清单(我个人的一个类别)显示,每个类别有一个检查箱。 当用户在检查箱上点击时,我走到一种方法。 我需要通过两个论点:

(1) the CheckChangedEventArg, which I pass, and
(2) the object that we re looking at, "Video", which I can t figure out how to pass along with the CheckChangedEventArg.

The XAML:

<CollectionView ItemsSource="VideoList">
  ...
    <DataTemplate x:DataType="model:Video">
        ...
               <CheckBox>
                   <CheckBox.Behaviors>
                       <toolkit:EventToCommandBehavior
                            x:TypeArguments="CheckedChangedEventArgs"
                            EventName="CheckedChanged"
                            Command="{Binding Source={x:Reference this}, Path=BindingContext.AddRemoveFromListCommand}"/>
                  </CheckBox.Behaviors>
              </CheckBox>
       ...
   </DataTemplate>
  ...
</CollectionView>

The ViewModel:

[RelayCommand]
public void AddRemoveFromList(CheckedChangedEventArgs args)
{
    if (args.Value)
    {
          // do the thing
    }
    else
    {
          // do the other thing
    }
}

请注意:working Code,但只有ChangedEVentArg。 我需要通过<代码>ChangedEventArgs和Video(在<DataTemplate/>上注明)。

What have I tried?

我尝试了reading the docs。 答案可能在于此,但我无法找到答案。 <代码>xTypeArguments是复数,使我想也许我可以补充另一个论点?

And, alas, I have access to the Video object in that attribute! enter image description here

这使我想,也许我需要做的是:

<CheckBox.Behaviors>
    <toolkit:EventToCommandBehavior
            x:TypeArguments="CheckedChangedEventArgs, Video (model)"
            EventName="CheckedChanged"
            Command="{Binding Source={x:Reference this}, Path=BindingContext.AddRemoveFromListCommand}" 
            CommandParameter="{Binding VideoFilename}"
            />
</CheckBox.Behaviors>

而且:

    [RelayCommand]
    public void AddRemoveFromList(CheckedChangedEventArgs args, Video video)
    {
            ...
    }

But that doesn t work.

I did a ton of other stuff, too, involving CommandParameter, but nothing worked.

是否有任何人知道在这方面做什么?

问题回答

你的代码必然如此复杂,你无需同时通过<条码>CeckedChangedEventArg和<条码>。 我们可以通过整个视频项目(包括核对表Box的价值)。

您仅可在视频模型中添加一个 b变(例如public bool IsChecked,并为你的视频项目(例如Item.cs)实施<条码>,然后将其与本编码上的财产相挂钩。

一旦我们核对或核对<代码>CheckBox,这一变量的价值将予以更新。 因此,我们可以通过整个视频项目。

I have achieved this function, you can refer to the following code: Suppose the Videl Model is Item.cs.

Item.cs

public class Item: INotifyPropertyChanged
{
    public string SectionName { get; set; }


   //Add a bool variable to indicate whether the CheckBox is checked
    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;
}

MyViewModel.cs

在这里,我们能够用代码<代码>获得整个项目。 Update thisItemCommand = new Front<Item>check Boxcommand;

public class MyViewModel: INotifyPropertyChanged
   {
       public ObservableCollection<Item> Sections { get; set; } = new ObservableCollection<Item>();

        // add this command to bind to CheckBox
       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" });


           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}");

               }

           }
       }
          

       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;

   }

Usage example:

<?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>

    </VerticalStackLayout>

</ContentPage>




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签