Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.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# 让WPF UserControl删除自身和绑定到它的数据对象?_C#_Wpf_User Controls - Fatal编程技术网

C# 让WPF UserControl删除自身和绑定到它的数据对象?

C# 让WPF UserControl删除自身和绑定到它的数据对象?,c#,wpf,user-controls,C#,Wpf,User Controls,我在我的主窗口中放置了一个StackPanel,它在运行时动态获取新的UserControl(UserControl是一行文本框和一个名为“Delete”的按钮)。 这是关于我如何创建UserControls的: PersonObject p = new PersonObject; List.Add(p); UserControlLine usrCtrlLine = new UserControlLine(); usrCtrlLine.DataContext = p; StackPanel.

我在我的主窗口中放置了一个StackPanel,它在运行时动态获取新的UserControl(UserControl是一行文本框和一个名为“Delete”的按钮)。 这是关于我如何创建UserControls的:

PersonObject p = new PersonObject;
List.Add(p);

UserControlLine usrCtrlLine = new UserControlLine();
usrCtrlLine.DataContext = p;

StackPanel.Children.Add(usrCtrlLine);
现在UserControl包含如下文本框:
TextBox TextWrapping=“Wrap”Grid.Column=“1”Text=“{Binding Path=Firstname,Mode=TwoWay}”

我的问题是,如何让用户控制
-从StackPanel中删除自身(“删除”)
-是否删除绑定到它的PersonObject p


非常感谢

我不确定我是否明白你在这里想做什么。。。是否要在StackPanel中显示人员列表?您应该使用ItemsControl,将其ItemsPanel定义为StackPanel,将其ItemTemplate定义为UserControlLine:

<ItemsControl ItemsSource="{Binding ListOfPersons}">
  <ItemsControl.ItemsPanel>
    <ItemsPanelTemplate>
      <StackPanel IsItemsHost="True"/>
    </ItemsPanelTemplate>
  </ItemsControl.ItemsPanel>
  <ItemsControl.ItemTemplate>
    <DataTemplate>
      <my:UserControlLine/>
    </DataTemplate>
  </ItemsControl.ItemTemplate>
</ItemsControl>


要删除项目,只需将其从人员集合中删除,关联的UserControlLine也将从ItemsControl中删除(集合应为ObservableCollection)

感谢您的输入!我用谷歌搜索了更多你的关键词,在这里找到了很好的例子: