English 中文(简体)
如何从收集角度删除选定的项目
原标题:How to delete selected Item from collection view in .net MAUI
  • 时间:2022-11-16 12:37:41
  •  标签:
  • .net
  • maui

我的儿童认为,我收集了资料。

 <CollectionView  SelectionMode="Single" SelectedItem="{Binding Source={Reference sideview}, Path=myViewModel.SelectedItem.FileName}"
                     ItemsSource="{Binding Source={x:Reference sideview}, Path=myViewModel.Items }" >
            <CollectionView.ItemTemplate>
            <DataTemplate >
                <Grid>
                <Label Text="{Binding FileName}"   VerticalOptions="Center"/>
                </Grid>
            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>

I want to delete selected item My parent view where I have delete button

      <Button x:Name="BTN_REMOVE_FILE"  Text="Remove" Command="{Binding DeleteCommand}" CommandParameter="{Binding SelectedItem}" >

I have created delete command in my viewmodel

   [RelayCommand]
    public void Delete(Data s)
    {
        if (Items.Contains(s)) {
            Items.Remove(s);
        }  
    }

从我看来,我已经从指挥参数的角度来看待了。

我还在我看来建立了选定项目模式。

 public Data selectedItem;
    public Data SelectedItem
    {
        get
        {
            return selectedItem;
        }
        set
        {
            if(selectedItem != value)
            {
                selectedItem = value;
            }
        }
    }`public MyViewModel()
    {

        Items = new ObservableCollection<Data>();
        selectedItem = new Data();
    }
    `
   

它表明我有例外,例如“直径”的“直径”(目标)不能是类型。 DemoApp.MVVM.ViewModel.MyViewModel,因为指挥类型需要辨别Dap。 MVVM.Model。 数据(参数参数)

在我看来,添加这一句 数据名称{到处;

参考: <Button x:Name=“BTN_REMOVE_FILE” 文本=“Remove”突击=“{具有约束力的删除Command}” 指挥Paraile=“{具有约束力的名称}”

最佳回答

为<条码>创建财产 您认为,

public Data selectedItem;
public Data SelectedItem
{
    get
    {
        return selectedItem;
    }
    set
    {
        if (selectedItem != value)
            selectedItem = value;
    }
}

3. 规定,从概念角度看,财产为:

public MyViewModel()
{
    Items = new ObservableCollection<Data>();
    SelectedItem = new Data();
}

然后,制定删除方法并通过选定项目:

public void Delete()
{
    if (Items.Contains(SelectedItem))
        Items.Remove(SelectedItem);
}

最后,对选定的公司具有约束力 收集观点的财产:

<CollectionView  SelectionMode="Single" SelectedItem="{Binding Source={Reference sideview}, Path=myViewModel.SelectedItem}">

当你使用<代码>代号()方法时,选定项目被删除。 例如,我对这种方法有了一个子:

<Button Text="Remove selected item" Command="{Binding DeleteCommand}" />
问题回答
Command="{Binding Source={RelativeSource AncestorType={x:Type viewmodel:MyViewModel}}, Path=DeleteCommand}"

澄清一个范围:

您必须留在数据表内(根据您的收件编号项目图2)。

{Binding .}

作为突击性参数。

要想在这种数据表内,你必须走一等,以便你找到你在你的视野中指挥的道路。

您可使用以下代码在收集意见中删除该项目。

In the Mainpage.xaml

<CollectionView ItemsSource="{Binding Products}">
        <CollectionView.ItemTemplate>
            <DataTemplate>
                
                    <StackLayout>
                        <Label Text="{Binding Name}" />
                        <Label Text="{Binding Price}" />
                        <Button Text="Remove" Clicked="Remove_Clicked" />
                    </StackLayout>
                
            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>

In the Mainpage.xaml.cs

  public partial class MainPage : ContentPage
    {
        public MainPage()
        {
            InitializeComponent();
            BindingContext = new ProductsViewModels();
        }
        public void Remove_Clicked(object sender, EventArgs e)
        {
            var button = sender as Button;
            var product = button.BindingContext as Products;
            var vm = BindingContext as ProductsViewModels;
            vm.RemoveCommand.Execute(product);
        }
    }

In the ViewModel

 internal class ProductsViewModels
    {
        public ObservableCollection<Products> Products
        {
            get;
            set;
        }
        public Command<Products> RemoveCommand
        {
            get
            {
                return new Command<Products>((Product) => {
                    Products.Remove(Product);
                });
            }
        }
        public ProductsViewModels()
        {
            Products = new ObservableCollection<Products> {
                new Products {
                    Name = "name1",
                        Price = 100
                },
                new Products {
                    Name = "name2",
                        Price = 100
                },
                new Products {
                    Name = "name3",
                        Price = 100
                }
            
            };
        }
    }




相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

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

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签