В очередной раз столкнулся с проблемой.
Есть модель
namespace WPF.Model
{
class PriceList
{
public List<Material> Materials { get; set; }
public PriceList()
{
Materials = new List<Material>();
}
}
}
Есть к ней некая VM
namespace WinDoorsWPF.ViewModel
{
class Price : INotifyPropertyChanged
{
PriceList pList = new PriceList();
public PriceList PList
{
get { return pList; }
set { pList = value; OnPropertyChanged("pList"); }
}
public Price()
{
}
public void GetPricesGoogle()
{
}
В MainWindow
Price price = new Price();
price.GetPricesGoogle();
После нажатия на кнопку, во втором окне должен отобразится уже инициализированный Price. Но в нем создается новый объект. И соответственно то, что нужно не отображается.
XAML второго окна:
<Window x:Class="WinDoorsWPF.View.PriceListWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WinDoorsWPF.View"
xmlns:viewModel = "clr-namespace:WinDoorsWPF.ViewModel"
mc:Ignorable="d"
Title="PriceListWindow" Height="450" Width="800">
<Grid>
<DataGrid Name="priceListGrid" ItemsSource="{Binding PList.Materials}"
HorizontalAlignment="Left" Height="215" Margin="160,105,0,0"
VerticalAlignment="Top" Width="568">
</DataGrid>
</Grid>
</Window>
А дальше
public PriceListWindow()
{
InitializeComponent();
DataContext = new Price() ;
}
Есть конечно идея создать static поле в VM и обращаться к нему напрямую, но что-то мне подсказывает, что так делать не надо.
На данный момент в моем понимании, что я где-то упустил понимание MVVM, что по сути весь код должен выполнятся в VM. Но я не понимаю, каким образом создать объект, чтобы можно было как в winform передавать его. Ну или обращаться к нему.
Понимаю, что во View должен быть по сути только код Xaml.