Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/25.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# 基于带有删除按钮的数据类型的Listview项_C#_Wpf_Mvvm - Fatal编程技术网

C# 基于带有删除按钮的数据类型的Listview项

C# 基于带有删除按钮的数据类型的Listview项,c#,wpf,mvvm,C#,Wpf,Mvvm,我有一个类似regex的搜索模式,这个模式由Variable或Literal类型的对象组成。我需要像这样一行一行地显示这些对象: 变量为蓝色,文本为红色。我定义了两个DataTemplates来设置颜色 <DataTemplate DataType="{x:Type local:Literal}"> <Border BorderThickness="1" BorderBrush="Red" Padding=&q

我有一个类似regex的搜索模式,这个模式由
Variable
Literal
类型的对象组成。我需要像这样一行一行地显示这些对象:

变量
为蓝色,
文本
为红色。我定义了两个
DataTemplate
s来设置颜色

<DataTemplate DataType="{x:Type local:Literal}">
   <Border BorderThickness="1" BorderBrush="Red" Padding="5" Margin="0 0 1 0"
           Background="Black">
      <TextBox Text="{Binding Text}" />
   </Border>
</DataTemplate>
<DataTemplate DataType="{x:Type local:Variable}">
   <Border BorderThickness="1" BorderBrush="Blue" Padding="5" Margin="0 0 1 0"
           Background="Black">
      <TextBox Text="{Binding Text}" />
   </Border>
</DataTemplate>

RegExList
是这些对象所在的位置。

您可以向包含
RegExList
的视图模型添加命令以删除项目

有不同的
ICommand
实现。如果您没有具体的命令类型,则可以从中获取。
RelayCommand
接受一个要执行的方法和一个返回是否可以使用给定参数执行命令的方法。如果您不熟悉命令,可以查看


请注意,您的
RegExList
集合应该是,否则从集合中删除项目不会反映在您的
ListView

中。您可以将命令添加到包含
RegExList
的视图模型中以删除项目

有不同的
ICommand
实现。如果您没有具体的命令类型,则可以从中获取。
RelayCommand
接受一个要执行的方法和一个返回是否可以使用给定参数执行命令的方法。如果您不熟悉命令,可以查看


请注意,您的
RegExList
集合应该是一个,否则从集合中删除项目不会反映在您的
列表视图中

添加
按钮
数据模板?您知道命令吗?添加
按钮
数据模板?你知道命令吗?
<ListView ItemsSource="{Binding RegExList}" Margin="5" Grid.Column="0"
          HorizontalAlignment="Left" VerticalAlignment="Center">
   <ItemsControl.ItemsPanel>
      <ItemsPanelTemplate>
         <WrapPanel Orientation="Horizontal" />
      </ItemsPanelTemplate>
   </ItemsControl.ItemsPanel>
</ListView>
public class MyViewModel : INotifyPropertyChanged
{
   public ICommand DeleteVariable { get; }
   public ICommand DeleteLiteral { get; }

   // ...your RegExList collection, other properties.

   public MyViewModel()
   {
      DeleteVariable = new RelayCommand<Variable>(ExecuteDeleteVariable, CanExecuteDeleteVariable);
      DeleteLiteral = new RelayCommand<Literal>(ExecuteDeleteLiteral, CanExecuteDeleteLiteral);
   }

   private void CanExecuteDeleteVariable(Variable variable)
   {
      // Optionally add conditions on when deletion is allowed
      return true;
   }

   private void ExecuteDeleteVariable(Variable variable)
   {
      RegExList.Remove(variable);
   }

   private void CanExecuteDeleteLiteral(Literal literal)
   {
      // Optionally add conditions on when deletion is allowed
      return true;
   }

   private void ExecuteDeleteLiteral(Literal literal)
   {
      RegExList.Remove(literal);
   }
}
<DataTemplate DataType="{x:Type local:Literal}">
   <Border BorderThickness="1" BorderBrush="Red" Padding="5" Margin="0 0 1 0"
                        Background="Black">
      <StackPanel Orientation="Horizontal">
         <TextBox Text="{Binding Text}" />
         <Button Content="X"
                 Command="{Binding DataContext.DeleteVariable, RelativeSource={RelativeSource AncestorType={x:Type ListView}}}"
                 CommandParameter="{Binding}"/>
      </StackPanel>      
   </Border>
</DataTemplate>
<DataTemplate DataType="{x:Type local:Variable}">
   <Border BorderThickness="1" BorderBrush="Blue" Padding="5" Margin="0 0 1 0"
                        Background="Black">
      <StackPanel Orientation="Horizontal">
         <TextBox Text="{Binding Text}" />
         <Button Content="X"
                 Command="{Binding DataContext.DeleteLiteral, RelativeSource={RelativeSource AncestorType={x:Type ListView}}}"
                 CommandParameter="{Binding}"/>
      </StackPanel>  
   </Border>
</DataTemplate>