при попытке выполнить ICommand всегда выдает исключение из-за того, что параметр имеет значение null.
Когда я вручную печатаю переменную, передаваемую в ICommand с помощью Debug.WriteLine, она работает отлично.
Это сама команда
private CatalogBase<T> _catalogBase;
public DeleteCommand()
{
_catalogBase = new CatalogBase<T>();
}
public bool CanExecute(object parameter)
{
return true;
}
public void Execute(object parameter)
{
_catalogBase.Delete((int)parameter);
}
public event EventHandler CanExecuteChanged;
Кнопка с привязанным параметром и командой:
<AppBarButton Icon="Delete" Label="Delete" CommandParameter="{Binding CustomerId}" Command="{Binding DeleteCommand}"/>
И это CustomerId , на который ссылаются выше:
public int CustomerId
{
get { return _customer.CustomerId; }
}
Опять же, ручная печать _customer.CustomerId возвращает правильный, а не нулевой.
Кто-нибудь знает, что может вызвать это?Заранее спасибо.
Полный xaml и viewmodel ниже:
<Page
x:Class="Hovmand.View.Domain.CustomersPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:page="using:Hovmand.ViewModel.Page"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d">
<Page.DataContext>
<page:CustomerViewModel/>
</Page.DataContext>
<Page.BottomAppBar>
<CommandBar>
<AppBarButton Icon="Add" Label="Create" />
<AppBarButton Icon="Edit" Label="Edit" />
<AppBarButton Icon="Delete" Label="Delete" DataContext="" CommandParameter="{Binding CustomerDomainObject.CustomerId}" Command="{Binding DeleteCommand}"/>
</CommandBar>
</Page.BottomAppBar>
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
<ListView ItemsSource="{Binding Customers}" SelectedItem="{Binding CustomerDomainObject, Mode=TwoWay}">
<ListView.HeaderTemplate>
<DataTemplate>
<Grid Padding="0,12,0,12" >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" Padding="20,0,0,0" Text="Name" Style="{ThemeResource CaptionTextBlockStyle}"/>
<TextBlock Grid.Column="1" Text="Phone" Style="{ThemeResource CaptionTextBlockStyle}"/>
<TextBlock Grid.Column="2" Text="Mail" Style="{ThemeResource CaptionTextBlockStyle}"/>
<TextBlock Grid.Column="3" Text="Company" Style="{ThemeResource CaptionTextBlockStyle}"/>
<TextBlock Grid.Column="4" Text="Adress" Style="{ThemeResource CaptionTextBlockStyle}"/>
</Grid>
</DataTemplate>
</ListView.HeaderTemplate>
<ListView.ItemTemplate>
<DataTemplate>
<Grid Height="48">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="1*"/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Column="0" VerticalAlignment="Center" Text="1" />
<TextBlock Grid.Column="1" VerticalAlignment="Center" Text="2"/>
<TextBlock Grid.Column="2" VerticalAlignment="Center" Text="3"/>
<TextBlock Grid.Column="3" VerticalAlignment="Center" Text="4"/>
<TextBlock Grid.Column="4" VerticalAlignment="Center" Text="5"/>
</Grid>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</Grid>
Viewmodel
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Windows.Input;
using Hovmand.Annotations;
using Hovmand.Model.Database;
using Hovmand.ViewModel.Base;
using Hovmand.ViewModel.Commands;
namespace Hovmand.ViewModel.Page
{
public class CustomerViewModel
{
public event PropertyChangedEventHandler PropertyChanged;
private ICommand _createCommand = new CreateCommand<Customer>();
private ICommand _deleteCommand = new DeleteCommand<Customer>();
private ICommand _updateCommand = new UpdateCommand<Customer>();
private HovmanddbContext dbContext = HovmanddbContext.Instance;
private Customer _customer = new Customer();
public CustomerViewModel()
{
}
public Customer CustomerDomainObject
{
get { return _customer; }
set
{
_customer = value;
OnPropertyChanged();
Debug.WriteLine(_customer.CustomerId);
}
}
public int CustomerId
{
get { return _customer.CustomerId; }
}
public int Cvr
{
get { return CustomerDomainObject.Cvr; }
set
{
CustomerDomainObject.Cvr = value;
OnPropertyChanged();
}
}
public string CompanyName
{
get { return CustomerDomainObject.CompanyName; }
set
{
CustomerDomainObject.CompanyName = value;
OnPropertyChanged();
}
}
public string Address
{
get { return CustomerDomainObject.Address; }
set
{
CustomerDomainObject.Address = value;
OnPropertyChanged();
}
}
public int Phone
{
get { return CustomerDomainObject.Phone; }
set
{
CustomerDomainObject.Phone = value;
OnPropertyChanged();
}
}
public string Mail
{
get { return CustomerDomainObject.Mail; }
set
{
CustomerDomainObject.Mail = value;
OnPropertyChanged();
}
}
public int FkContactId
{
get { return CustomerDomainObject.FkContactId; }
set
{
CustomerDomainObject.FkContactId = value;
OnPropertyChanged();
}
}
public int FkLocationId
{
get { return CustomerDomainObject.FkLocationId; }
set
{
CustomerDomainObject.FkLocationId = value;
OnPropertyChanged();
}
}
public List<Customer> Customers
{
get { return dbContext.Customers.ToList(); }
}
public ICommand CreateCommand
{
get { return _createCommand; }
}
public ICommand DeleteCommand
{
get { return _deleteCommand; }
}
public ICommand UpdateCommand
{
get { return _updateCommand; }
}
[NotifyPropertyChangedInvocator]
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}