如何在子控件级别从集合中删除元素

问题描述

我已经创建了UserControl来使用按钮添加/删除属性。它具有带ObservableCollection属性的ItemsControl,作为viewmodel绑定到该属性,并具有将新属性添加到集合的按钮。每个新属性都会在集合中创建一个实体,该实体也是一个具有绑定属性viewmodel和用于删除自身的按钮的UserControl。

我想编写此功能,以便单个属性控件(和其他控件)可以重用-并在一些地方使用了它。当前添加属性可以正常工作,但是我找不到删除按钮正常工作并从集合中删除对象的任何工作方式。

我尝试通过 this.Parent 进行引用,但是 CustomAttributeControl 的父级为null,我试图通过Tag进行引用或使用Prism.Core,但我无法不能正确执行任何操作以使它正常工作,而我找不到任何具有类似嵌套控件问题的依据。当没有单个属性的嵌套控件时,这很容易,但是在这里我什么都没想。我对WPF还是很陌生,我觉得应该有一种简单的方法来做到这一点。

single attribute control

属性控件的XAML:

<UserControl x:Class="xxx.UI.CustomAttributeControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:models="clr-namespace:xxx.viewmodels.Attributes"
             mc:Ignorable="d" >
  <DockPanel Width="auto" HorizontalAlignment="Stretch" Margin="0,2">
    <TextBox Text="{Binding Name}" Height="20" textwrapping="Wrap" Width="160"/>
    <TextBlock Width="60"/>
    <ComboBox x:Name="DataType" Height="20" Width="120" SelectionChanged="DataType_SelectionChanged"/>
    <TextBlock Width="10"/>
    <TextBox Text="{Binding ListValues}" x:Name="ListValues"  Height="20" textwrapping="Wrap" Width="120"/>
    <DockPanel HorizontalAlignment="Right">
      <CheckBox IsChecked="{Binding Visible}" VerticalAlignment="Center" HorizontalAlignment="Right" Margin="20,0"/>
      <Button x:Name="Delete" Content="X" Width="22" Height="20" VerticalAlignment="Center" HorizontalAlignment="Right" Margin="20,0"/>
    </DockPanel>
  </DockPanel>
</UserControl>

list control

列表控件的XAML:

<UserControl x:Class="xxx.UI.CustomAttributesControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:xxx.UI"
             mc:Ignorable="d">
  <StackPanel>
    <Label FontSize="20">Custom attributes</Label>
    <StackPanel>
      <DockPanel HorizontalAlignment="Stretch">
        <Label Width="160" HorizontalAlignment="Left" HorizontalContentAlignment="Center">Attribute name</Label>
        <TextBlock Width="60"/>
        <Label Width="120" HorizontalAlignment="Left" HorizontalContentAlignment="Center">Attribute type</Label>
        <TextBlock Width="10"/>
        <Label Width="120" HorizontalAlignment="Left" HorizontalContentAlignment="Center">List values</Label>
        <DockPanel HorizontalAlignment="Right">
          <Label Width="60" HorizontalAlignment="Right" HorizontalContentAlignment="Center">Visible</Label>
          <Label Width="60" HorizontalAlignment="Right" HorizontalContentAlignment="Center">Delete</Label>
        </DockPanel>
      </DockPanel>
      <ItemsControl ItemsSource="{Binding}"> <!-- it's binding ObservableCollection of custom attributes from parent control -->
        <ItemsControl.ItemTemplate>
          <DataTemplate>
            <local:CustomAttributeControl />
          </DataTemplate>
        </ItemsControl.ItemTemplate>
      </ItemsControl>
      <DockPanel HorizontalAlignment="Right">
        <Button Margin="20,10" Click="AddAttribute">+ Add attribute</Button>
      </DockPanel>
    </StackPanel>
  </StackPanel>
</UserControl>

在.xaml.cs中添加属性代码

        private void AddAttribute(object sender,RoutedEventArgs e)
        {
            var attributes = (ObservableCollection<CustomAttribute>)DataContext;
            attributes.Add(new CustomAttribute());
        }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)