我正在用c#开发windows 10通用应用程序。 我有一个UserControl是MyListview项目模板。 Listview将绑定数据。在userControl中,有一个用于删除usercontrol DependencyProperty Content(包含字符串Text,Name和int Id)的按钮。如何通过UserControl模板中的函数删除ListView项目?
Listview显示对象的文本和删除它的按钮。
现在如何通过单击删除按钮从我的列表中删除该项目?
更新
我的数据类:
class Data
{
public int Id { get; set; }
public string Text { get; set; }
}
我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));
用户控件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>
,并在代码behinde页我用一个ObservableCollection<Data> items = new ObservableCollection<Data>();
设置Listview.ItemsSource
它。
的主要问题是如何从items
删除MyUsercontrol1
所以你不要如何实现Remove_Click,你呢? –
@MichałKomorowski是的,我想按删除按钮和项目从列表中删除 –