不要触发RelayCommand

时间:2019-04-10 11:11:45

标签: wpf mvvm-light

我有一个列表,并具有中继命令视图模型,

private List<PurchaseInvoiceDetail> detailList = new List<PurchaseInvoiceDetail>();
public RelayCommand AddCommand { get; set; }
   ICollectionView _detailCollection;

    public ICollectionView DetailCollection
    {
        get { return _detailCollection; }
        set
        {
            if (value != _detailCollection)
            {
                _detailCollection = value;
                RaisePropertyChanged("DetailCollection");
            }
        }
    }

当我在构造函数中使用DetailCollection时,请勿触发火电继电器命令。

public PurchaseInvoiceDetailViewModel(PurchaseInvoice item)
 {
  if (item != null)
   {
    Number = item.Number;
    CustomerId = item.CustomerId;
    Date = new PersianDate((DateTime)item.Date);
    _purchaseInvoice = item.Id;
    detailList = _db.PurchaseInvoiceDetails.Where(m => m.PurchaseInvoiceId == item.Id).ToList();
    CollectionViewSource.GetDefaultView(detailList).Refresh();
    DetailCollection = CollectionViewSource.GetDefaultView(detailList);
   }
   AddCommand=new RelayCommand(() =>
        {
              var goods = _db.Goods.Find(GoodsId);
                detailList.Add(new PurchaseInvoiceDetail(){GoodsId = GoodsId,Goods = goods,Count = Count,Price = Price,Id=0,PurchaseInvoiceId = 0});
                CollectionViewSource.GetDefaultView(detailList).Refresh();
                DetailCollection = CollectionViewSource.GetDefaultView(detailList);

        });
  }

但是当我评论DetailCollection时,它就可以了。

可见

 <Button Margin="0 0 5 0 " Width="90" Command="{Binding AddCommand}" 
   Style="{StaticResource MaterialDesignRaisedButton}"  >
  <StackPanel Orientation="Horizontal">
    <materialDesign:PackIcon  Kind="Plus" />
    <TextBlock >َAdd</TextBlock>
  </StackPanel>
 </Button>
 <DataGrid  CanUserDeleteRows="True" CellStyle="{StaticResource MaterialDesignDataGridCell}"  Name="ListView" 
 IsReadOnly="True" BorderBrush="{DynamicResource MaterialDesignDivider}"  VerticalAlignment="Top" BorderThickness="1"    Height="200"  Margin="0"   AutoGenerateColumns="False"   
                       ItemsSource="{Binding Path=DetailCollection}" 
            >
     <DataGrid  CanUserDeleteRows="True" CellStyle="{StaticResource MaterialDesignDataGridCell}"  Name="ListView" 
                       IsReadOnly="True" BorderBrush="{DynamicResource MaterialDesignDivider}"  VerticalAlignment="Top"
                       BorderThickness="1"    Height="200"  Margin="0"   AutoGenerateColumns="False"   
                       ItemsSource="{Binding Path=DetailCollection}" 
            >
                <DataGrid.Columns>
                    <DataGridTextColumn  Width="100"
                                         Header="کد کالا" Binding="{Binding Path=Goods.Code}"/>

                    <DataGridTextColumn Header="نام کالا" Binding="{Binding Path=Goods.Name}" Width="200"/>

                    <DataGridTextColumn Binding="{Binding Path=Count}" Width="150"
                                        dataGridFilterLibrary:DataGridColumnExtensions.IsCaseSensitiveSearch="True"
                                        Header="تعداد"/>
                    <DataGridTextColumn Header="قیمت" Binding="{Binding Path=Price}" Width="*"/>
                </DataGrid.Columns>
                <DataGrid.InputBindings>
                    <KeyBinding 
                        Key="Delete"
                        Command="{Binding DeleteCommand}" CommandParameter="{Binding SelectedIndex, ElementName=ListView}"/>

                </DataGrid.InputBindings>
            </DataGrid>

0 个答案:

没有答案