[英]How to bring user-added item to dropdownlist after page refresh in asp.net?
[英]How refresh view after added new item
我有问题,因为如果我在我的 observablecollection 中添加新项目,我在我的视图中看不到结果。 我需要重新启动,然后才能看到新项目。
这是我显示项目的视图模型
public class ManageFleetListingViewModel : ViewModelBase
{
private readonly Func<IEnumerable<DisplayManageFleetViewModel>,IEnumerable<DisplayManageFleetViewModel>> _filtersVehicle;
private readonly ObservableCollection<DisplayManageFleetViewModel> _manageFleetViewModel;
private readonly VehicleState _vehicleState;
private readonly IManageFleetService _manageFleetService;
public IEnumerable<DisplayManageFleetViewModel> Vehicles => _manageFleetViewModel;
public ICommand DeleteVehicleCommand { get; set; }
public ManageFleetListingViewModel(VehicleState vehicleState, IManageFleetService manageFleetService) : this(vehicleState,manageFleetService, manageFleet => manageFleet) { }
public ManageFleetListingViewModel(VehicleState vehicleState, IManageFleetService manageFleetService, Func<IEnumerable<DisplayManageFleetViewModel>, IEnumerable<DisplayManageFleetViewModel>> filtersVehicle )
{
DeleteVehicleCommand = new DeleteVehicleCommand(this, manageFleetService);
_filtersVehicle = filtersVehicle;
_vehicleState = vehicleState;
_manageFleetViewModel = new ObservableCollection<DisplayManageFleetViewModel>();
_vehicleState.StateChanged += VehicleState_StateChanged;
DisplayVehicles();
}
public void DeleteItem(int id)
{
var item = Vehicles.FirstOrDefault(x => x.Id == id);
_manageFleetViewModel.Remove(item);
}
public void AddItem()
{
DisplayVehicles();
}
private void DisplayVehicles()
{
IEnumerable<DisplayManageFleetViewModel> displayManageFleets = _vehicleState.GetVehicles
.Select(s => new DisplayManageFleetViewModel(s.Id, s.CarBrand, s.VIN, s.Milage, s.EnigneNumber, s.EngineCapacity, s.RegistrationNumber, s.FirstRegistration, s.YearPurchase, s.YearProduction, s.ImageCar));
displayManageFleets = _filtersVehicle(displayManageFleets);
_manageFleetViewModel.Clear();
foreach (DisplayManageFleetViewModel viewModel in displayManageFleets)
{
_manageFleetViewModel.Add(viewModel);
}
}
private void VehicleState_StateChanged()
{
DisplayVehicles();
}
这是我添加项目的项目域
public class ManageFleetService : IManageFleetService
{
private readonly IDataService<Account> _accountService;
private readonly IVehicleService _vehicleService;
public ManageFleetService(IDataService<Account> accountService, IVehicleService vehicleService)
{
_accountService = accountService;
_vehicleService = vehicleService;
}
public async Task<Account> AddVehicle(string carBrand, string vin, string milage, string engineNumber, string engineCapacity, string registerNumber, DateTime firstRegistration, DateTime yearPurchase, DateTime yearProduction, byte[] imageCar,Account accountId)
{
Vehicle vehicleVIN = await _vehicleService.GetByVIN(vin);
if(vehicleVIN != null)
{
throw new InvalidVinNumberException(vin);
}
Vehicle vehicleRegistraion = await _vehicleService.GetByRegistrationNumber(registerNumber);
if(vehicleRegistraion != null)
{
throw new InvalidRegistrationNumberException(registerNumber);
}
Vehicle vehicle = new Vehicle()
{
CarBrand = carBrand,
VIN = vin,
Milage = milage,
EnigneNumber = engineNumber,
EngineCapacity = engineCapacity,
RegistrationNumber = registerNumber,
FirstRegistration = firstRegistration,
YearPurchase = yearPurchase,
YearProduction = yearProduction,
ImageCar = imageCar,
Account = accountId
};
accountId.Vehciles = new List<Vehicle>();
accountId.Vehciles.Add(vehicle);
await _accountService.Update(accountId, accountId.Id);
return accountId;
}
将项目添加到数据库后,我将在下一次显示命令中的 DisplayVehicles 函数
Account account = await _manageFleetService.AddVehicle(carBrand, vin, milage, engineCapacity, engineCapacity, registerNumber, firstRegistration, yearPurchase, yearProduction, imageCar, _accountStore.CurrentAccount);
_manageFleetListingViewModel.AddItem();
这是我的 userconrol (ManageFleetListing) xaml,我在其中显示项目
<Grid>
<StackPanel HorizontalAlignment="Center">
<ItemsControl ItemsSource="{Binding Vehicles}">
<ItemsControl.ItemTemplate>
<DataTemplate>
<Task:VehcileTask/>
</DataTemplate>
</ItemsControl.ItemTemplate>
</ItemsControl>
</StackPanel>
</Grid>
这是我的 VehicleTask 模式
<StackPanel>
<Border>
</Border>
<StackPanel Orientation="Horizontal">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="auto"/>
<ColumnDefinition Width="auto"/>
</Grid.ColumnDefinitions>
</Grid>
<StackPanel Grid.Column="0" MinWidth="150" MaxWidth="300">
<TextBlock Text="{Binding CarBrand}" FontSize="12" FontWeight="DemiBold"/>
</StackPanel>
<StackPanel Grid.Column="1" MinWidth="150" MaxWidth="300">
<TextBlock Text="{Binding VIN}" FontSize="12" FontWeight="DemiBold"/>
</StackPanel>
<StackPanel Grid.Column="2" MinWidth="100" MaxWidth="200">
<TextBlock Text="{Binding Milage}" FontSize="12" FontWeight="DemiBold"/>
</StackPanel>
<StackPanel Grid.Column="3" MinWidth="100" MaxWidth="200" Margin="0 0 20 0">
<TextBlock Text="{Binding YearProduction, StringFormat='dd/MM/yyyy'}" FontSize="12" FontWeight="DemiBold"/>
</StackPanel>
<Button Content="Edit" Command="{Binding Path=DataContext.DeleteVehicleommand, RelativeSource={RelativeSource AncestorType=local:ManageFleetListing}}"/>
<Button Background="Red" BorderThickness="0" Content="Delete" Command="{Binding Path=DataContext.DeleteVehicleCommand, RelativeSource={RelativeSource AncestorType=local:ManageFleetListing}}" Margin="10 0 0 0" CommandParameter="{Binding Id}"/>
</StackPanel>
<Border BorderThickness="1" Background="Black"></Border>
</StackPanel>
这是这里的主要视图,我使用 CreateVehicleCommandand 并显示 ManageFleetListing
<Button
Command="{Binding CreateVehicleCommand}"
Style="{DynamicResource InventoryButton}" Height="50" Width="200" HorizontalAlignment="Left" Margin="40 20 0 0">
<Button.Background>
<LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
<GradientStop Color="#FF5DFF00"/>
<GradientStop Color="White" Offset="1"/>
</LinearGradientBrush>
</Button.Background>
<Button.Content>
<TextBlock Text="Add" FontSize="20" FontWeight="Bold" Foreground="Gray"/>
</Button.Content>
</Button>
<Grid Grid.Row="5" Height="200" Margin="0 10 0 0">
<ManageFleet:ManageFleetListing DataContext="{Binding ManageFleetListingViewModel}"/>
</Grid>
这是上面 xaml 的视图模型
public class ManageFleetViewModel : ViewModelBase
{
public ICommand CreateVehicleCommand { get; set; }
public ManageFleetListingViewModel ManageFleetListingViewModel { get; }
public ManageFleetViewModel(IManageFleetService menageFleetService, IAccountStore accountStore,VehicleState vehicleState, IManageFleetService manageFleetService,ManageFleetListingViewModel manageFleetListingViewModel)
{
CreateVehicleCommand = new CreateVehicleCommand(this, menageFleetService, accountStore, manageFleetListingViewModel);
ManageFleetListingViewModel = new ManageFleetListingViewModel(vehicleState,manageFleetService);
}
private string _carbrand { get; set; } //if is problem with added to database look here
private string _vin { get; set; }
private string _milage { get; set; }
private string _enigneNumber { get; set; }
private string _engineCapacity { get; set; }
private string _registrationNumber { get; set; }
private DateTime _firstRegistration { get; set; }
private DateTime _yearPurchase { get; set; }
private DateTime _yearProduction { get; set; }
private byte [] _imageCar { get; set; }
public string CarBrand
{
get
{
return _carbrand;
}
set
{
_carbrand = value;
OnPropertyChanged(nameof(CarBrand));
}
}
public string VIN
{
get
{
return _vin;
}
set
{
_vin = value;
OnPropertyChanged(nameof(VIN));
}
}
public string Milage
{
get
{
return _milage;
}
set
{
_milage = value;
OnPropertyChanged(nameof(Milage));
}
}
public string EnigneNumber
{
get
{
return _enigneNumber;
}
set
{
_enigneNumber = value;
OnPropertyChanged(nameof(EnigneNumber));
}
}
public string EngineCapacity
{
get
{
return _engineCapacity;
}
set
{
_engineCapacity = value;
OnPropertyChanged(nameof(EngineCapacity));
}
}
public string RegistrationNumber
{
get
{
return _registrationNumber;
}
set
{
_registrationNumber = value;
OnPropertyChanged(nameof(RegistrationNumber));
}
}
public DateTime FirstRegistration
{
get
{
if(_firstRegistration.Year == 1) { return DateTime.Now; }
return _firstRegistration;
}
set
{
_firstRegistration = value;
OnPropertyChanged(nameof(FirstRegistration));
}
}
public DateTime YearPurchase
{
get
{
if(_yearPurchase.Year == 1) { return DateTime.Now; }
return _yearPurchase;
}
set
{
_yearPurchase = value;
OnPropertyChanged(nameof(YearPurchase));
}
}
public DateTime YearProduction
{
get
{
if (_yearProduction.Year == 1) { return DateTime.Now; }
return _yearProduction;
}
set
{
_yearProduction = value;
OnPropertyChanged(nameof(YearProduction));
}
}
public byte [] ImageCar
{
get
{
return _imageCar;
}
set
{
_imageCar = value;
OnPropertyChanged(nameof(ImageCar));
}
}
以下是已接受的答案,但正如您从评论中看到的那样,我的陈述是不正确的。
您必须绑定到ObservableCollection<T>
以使控件订阅对集合的更改。 在您的情况下,您绑定到IEnumerable<T>
以便从此列表中填充控件一次。 控件永远不会看到对支持IEnumerable<T>
的ObservableCollection<T>
更改。
public IEnumerable<DisplayManageFleetViewModel> Vehicles => _manageFleetViewModel;
只需将其更改为
public ObservableCollection<DisplayManageFleetViewModel> Vehicles => _manageFleetViewModel;
或者甚至更好地摆脱_manageFleetViewModel
字段并更改Vehicles
:
public ObservableCollection<DisplayManageFleetViewModel> Vehicles { get; }
然后在您的代码中使用Vehicles
而不是_manageFleetViewModel
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.