Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何通过UserControl模板中的函数删除ListView项?_C#_Listview_User Controls_Win Universal App_Windows 10 Mobile - Fatal编程技术网

C# 如何通过UserControl模板中的函数删除ListView项?

C# 如何通过UserControl模板中的函数删除ListView项?,c#,listview,user-controls,win-universal-app,windows-10-mobile,C#,Listview,User Controls,Win Universal App,Windows 10 Mobile,我正在用c#开发windows 10通用应用程序。 我有一个UserControl,它是MyListview项模板。Listview将绑定数据。在userControl中,有一个用于删除userControl DependencyProperty内容(包含字符串文本、名称和int Id)的按钮 Listview显示对象的文本以及用于删除它的按钮 现在,如何通过单击“删除”按钮从我的列表中删除该项目 更新 我的数据类: class Data { public int Id

我正在用c#开发windows 10通用应用程序。 我有一个UserControl,它是MyListview项模板。Listview将绑定数据。在userControl中,有一个用于删除userControl DependencyProperty内容(包含字符串文本、名称和int Id)的按钮

Listview显示对象的文本以及用于删除它的按钮

现在,如何通过单击“删除”按钮从我的列表中删除该项目

更新

我的数据类:

class Data
    {
        public int Id { get; set; }
        public string Text { get; set; }
    }
my usercontrol.cs:

public Data Content
{
     get { return (Data)GetValue(ContentProperty); }
     set { SetValue(ContentProperty, value); }
}

// Using a DependencyProperty as the backing store for Content.  This enables animation, styling, binding, etc...
 public static readonly DependencyProperty ContentProperty =
            DependencyProperty.Register("Content", typeof(Data), typeof(MyUserControl1), new PropertyMetadata(null));
usercontrol xaml:

<StackPanel>
    <TextBlock x:Name="textBlock" Text="{Binding Content.Text, ElementName=textBlock}" />
    <Button Click="Remove_Click"/>
</StackPanel>

我的清单:

<Page.Resources>
      <DataTemplate x:Key="ListViewTemplate">
          <local:MyUserControl1 Content="{Binding}"/>
      </DataTemplate>
</Page.Resources>
<Grid>
   <ListView x:Name="ListView" ItemTemplate="{StaticResource ListViewTemplate}" />
</Grid>

在页面后面的代码中,我使用了一个
ObservableCollection items=newobservableCollection()
设置
Listview.ItemsSource


主要的问题是如何从MyUsercontrol1中的
项中删除该项,您写了关于绑定的文章,因此我假设在您的XAML中有以下代码或类似代码:

<ListView ItemSource = "{Bind SomeCollection"} ... />
OnRemoveCallback
将用于通知
ListView
应删除给定的数据元素
Remove\u单击
MyUserControl
中的处理程序,只需执行
OnRemove

private void Remove_Click(object sender, RoutedEventArgs e)
{
    Content.OnRemove();
}
最后,在
页面的代码隐藏中
我们必须定义一个逻辑,负责从列表中实际删除数据项:

public void Remove(Data d)
{
    ((ObservableCollection<Data>) ListView.ItemsSource).Remove(d);
}
公共作废删除(数据d)
{
((ObservableCollection)ListView.ItemsSource).删除(d);
}

ListView.ItemsSource=newObservableCollection()
{
新数据(){Id=1,Text=“1”,OnRemoveCallback=Remove},
新数据(){Id=2,Text=“2”,OnRemoveCallback=Remove}
};
现在,只要按下“删除”按钮,您的页面就会收到通知,并将执行一项操作


正如我所说,这不是一个完美的解决方案。就我个人而言,我将使用MVVM模式。感谢您将XAML和C#分开。

我使用的是ObservableCollection,但主要问题是如何从MyUserControle中删除它。@AliMalek您需要从集合中删除数据项,而不是从UserControls本身删除数据项。如果删除该项,列表将删除UserControl。但它将从ObservableCollection集合中删除。现在,我不明白你想要实现什么。所以你不知道如何实现删除单击,是吗?@MichałKomorowski是的,我想按下删除按钮,然后将项目从列表中删除
public void Remove(Data d)
{
    ((ObservableCollection<Data>) ListView.ItemsSource).Remove(d);
}
ListView.ItemsSource = new ObservableCollection<Data>()
{
    new Data() {Id = 1, Text = "1", OnRemoveCallback = Remove},
    new Data() {Id = 2, Text = "2", OnRemoveCallback = Remove}
};