Пустой Combobox WPF
Проблема в том, что когда делаю binding данных в XAML, то комбобоксы пустые, если же передать их в конструктор создаваемого окна и там напрямую сделать ItemSource = коллекция, то ситуация противоположная. Коллекции загружаются до создания окна и поэтому не могут быть пустыми, но почему тогда происходит такая ситуация?
internal class ProductForSaleViewModel : ViewModel
{
private readonly IProductForSaleService _service;
private readonly IProductService _productService;
private readonly IStoreService _storeService;
private readonly IProductDeliveryService _productDeliveryService;
private readonly IPackagingService _packagingService;
private ObservableCollection<ProductForSale> _productForSales;
private ObservableCollection<Product> _products;
private ObservableCollection<Store> _stores;
private ObservableCollection<ProductDelivery> _productDeliveries;
private ObservableCollection<Packaging> _packagings;
#region Команды
#region GetCommand
public ICommand GetCommand { get; }
private bool CanGetCommandExecute(object p) => true;
public async void OnGetCommandExecuted(object p)
{
ProductForSales = new ObservableCollection<ProductForSale>(await _service.Get());
}
#endregion
public ICommand AddEditCommand { get; }
private bool CanAddEditCommandExecute(object p) => true;
public async void OnAddEditCommandExecuted(object p)
{
await LoadComboBoxes();
var page = new AddEditProductForSalePage();
page.Show();
}
#endregion
public ObservableCollection<ProductForSale> ProductForSales
{
get => _productForSales;
set => Set(ref _productForSales, value);
}
public ObservableCollection<Product> Products
{
get => _products;
set => Set(ref _products, value);
}
public ObservableCollection<Store> Stores
{
get => _stores;
set => Set(ref _stores, value);
}
public ObservableCollection<ProductDelivery> ProductDeliveries
{
get => _productDeliveries;
set => Set(ref _productDeliveries, value);
}
public ObservableCollection<Packaging> Packagings
{
get => _packagings;
set => Set(ref _packagings, value);
}
public ProductForSaleViewModel(IProductForSaleService service,
IProductService productService,
IStoreService storeService,
IProductDeliveryService productDeliveryService,
IPackagingService packagingService)
{
_service = service;
_productService = productService;
_storeService = storeService;
_productDeliveryService = productDeliveryService;
_packagingService = packagingService;
GetCommand = new LambdaCommand(OnGetCommandExecuted);
AddEditCommand = new LambdaCommand(OnAddEditCommandExecuted);
_productForSales = new ObservableCollection<ProductForSale>();
OnGetCommandExecuted(null);
}
private async Task LoadComboBoxes()
{
await GetProduct();
await GetStores();
await GetProductDeliveries();
await GetPackagings();
}
private async Task GetProduct()
{
Products = new ObservableCollection<Product>(await _productService.Get());
}
private async Task GetStores()
{
Stores = new ObservableCollection<Store>(await _storeService.Get());
}
private async Task GetProductDeliveries()
{
ProductDeliveries = new ObservableCollection<ProductDelivery>(await _productDeliveryService.Get());
}
private async Task GetPackagings()
{
Packagings = new ObservableCollection<Packaging>(await _packagingService.Get());
}
}
Представление:
<Window x:Class="CandyKeeper.Presentation.Views.AddEditPages.AddEditProductForSalePage"
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:CandyKeeper.Presentation.Views.AddEditPages"
xmlns:iconPacks="http://metro.mahapps.com/winfx/xaml/iconpacks"
WindowStartupLocation="CenterScreen"
WindowStyle="None"
AllowsTransparency="True" Background="Transparent"
DataContext="{Binding ProductForSaleViewModel,Source={StaticResource Locator}}"
Title="AddEditProductForSalePage" Height="400" Width="300">
<Border Background="#dbdae0" CornerRadius="20"
MouseDown="Border_MouseDown" MouseLeftButtonDown="Border_MouseLeftButtonDown">
<Grid>
<StackPanel
VerticalAlignment="Center">
<Button Style="{StaticResource gridRemoveButton}" Click="CloseBtn" Width="25" Height="25" Margin="250 0 0 0">
<iconPacks:PackIconMaterial Kind="Close" Style="{StaticResource gridButtonIcon}"/>
</Button>
<TextBlock Text="Кондитерские изделия" Grid.Row="1" FontSize="14" FontWeight="SemiBold" Margin="0 10 0 0"
HorizontalAlignment="Center" Foreground="#121518"/>
<!-- Заполнение данных-->
<TextBlock Text="Продукт" Margin="50 20 0 0"></TextBlock>
<ComboBox Width="200" Name="ProductComboBox" ItemsSource="{Binding Products }" DisplayMemberPath="Name" SelectedValuePath="Id"></ComboBox>
<TextBlock Text="Магазин" Margin="50 0 0 0" ></TextBlock>
<ComboBox Width="200" ItemsSource="{Binding Stores}" DisplayMemberPath="Name" SelectedValuePath="Id"></ComboBox>
<TextBlock Text="Поставка" Margin="50 0 0 0"></TextBlock>
<ComboBox Width="200" ItemsSource="{Binding ProductDeliveries}" SelectedValuePath="Id"></ComboBox>
<TextBlock Text="Расфасовка" Margin="50 0 0 0"></TextBlock>
<ComboBox Width="200" ItemsSource="{Binding Packagings}" DisplayMemberPath="Name" SelectedValuePath="Id"></ComboBox>
<TextBlock Text="Цена" Margin="50 0 0 0"></TextBlock>
<TextBox Width="200"></TextBox>
<TextBlock Text="Объём" Margin="50 0 0 0"></TextBlock>
<TextBox Width="200"></TextBox>
<!--Сохранение-->
<Button Style="{StaticResource addButton}" Height="30" Width="100" Margin="100,40,100,0">
<StackPanel Orientation="Horizontal">
<iconPacks:PackIconMaterial Kind="ContentSave" VerticalAlignment="Center" Width="11" Height="11" Margin="0 1 8 0"/>
<TextBlock Text="Сохранить"/>
</StackPanel>
</Button>
</StackPanel>
</Grid>
</Border>