У меня есть окна WPF, имеющие следующий XAML код:
<Window x:Class="WpfApp1.ManagerWindow"
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:ClassLibrary3;assembly=ClassLibrary3"
d:DataContext="{d:DesignInstance Type=local:DailyReportsModelView}"
mc:Ignorable="d"
Title="ManagerWindow" Height="450" Width="863">
<Grid>
<TabControl>
<TabItem Header="Отчёты">
<Grid Background="#FFE5E5E5">
<ListBox ItemsSource="{Binding Reports}" SelectedItem ="{Binding SelectedReport}" Margin="0,0,429,0">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="5">
<TextBlock Text="{Binding Path=Locksmith}" />
<TextBlock Text="{Binding Path=Date}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<StackPanel Background="#FFE5E5E5" DataContext ="{Binding SelectedReport}" Margin="428,0,0,0">
<TextBlock Text="Выбранный элемент" FontSize="16" />
<TextBlock Text="Количество деталей" FontSize="16" />
<TextBox Text="{Binding DetailsCount, UpdateSourceTrigger=PropertyChanged}" FontSize="16"/>
<TextBlock Text="Дата" FontSize="16" />
<TextBox Text="{Binding Date, UpdateSourceTrigger=PropertyChanged}" FontSize="16" />
<TextBlock Text="Работник" FontSize="16" />
</StackPanel>
<ComboBox
ItemsSource="{Binding Locksmiths}"
SelectedItem="{Binding SelectedLocksmith, UpdateSourceTrigger=PropertyChanged}"
Margin="430,133,3,226"/>
<Button Content="Добавить отчёт" HorizontalAlignment="Left" Margin="465,352,0,0" VerticalAlignment="Top" Height="44" Width="365" FontSize="16" Click="Button_Click"/>
<Button Content="Сохрнаить текущий отчёт" HorizontalAlignment="Left" Margin="465,203,0,0" VerticalAlignment="Top" Height="44" Width="365" FontSize="16"/>
<Button Content="Удалить выбранный отчёт" HorizontalAlignment="Left" Margin="465,252,0,0" VerticalAlignment="Top" Height="44" Width="365" FontSize="16"/>
</Grid>
</TabItem>
</TabControl>
</Grid>
</Window>
<Window x:Class="WpfApp1.AddReportWindow"
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:ClassLibrary3;assembly=ClassLibrary3"
d:DataContext="{d:DesignInstance Type=local:DailyReportsModelView}"
mc:Ignorable="d"
Title="AddReportWindow" Height="226" Width="180">
<Grid>
<Label Content="Количество деталей" HorizontalAlignment="Left" Margin="10,0,0,0" VerticalAlignment="Top" FontSize="16"/>
<TextBox x:Name="DetailsCount" Text="{Binding ReportToAdd.DetailsCount}" HorizontalAlignment="Center" Margin="0,31,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="160" FontSize="16"/>
<Label Content="Дата отчёта" HorizontalAlignment="Left" Margin="10,54,0,0" VerticalAlignment="Top" FontSize="16"/>
<TextBox x:Name="ReportDate" HorizontalAlignment="Center" Margin="0,82,0,0" TextWrapping="Wrap" Text="{Binding ReportToAdd.Date, Mode=TwoWay}" VerticalAlignment="Top" Width="160" FontSize="16"/>
<ComboBox x:Name="LocksmithSelect" ItemsSource="{Binding Locksmiths}" SelectedItem="{Binding ReportToAdd.Locksmith, Mode=TwoWay}" HorizontalAlignment="Center" Margin="0,110,0,0" VerticalAlignment="Top" Width="160" FontSize="16"/>
<Button Command="{Binding AddCommand}" CommandParameter="{Binding ReportToAdd}" Content="Добавить" HorizontalAlignment="Center" Margin="0,153,0,0" VerticalAlignment="Top" Height="34" Width="159" Click="Button_Click"/>
</Grid>
</Window>
Также у меня есть ViewModel:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
namespace ClassLibrary3
{
public class DailyReportsModelView : INotifyPropertyChanged
{
private Report _selectedReport;
private Locksmith _selectedLocksmith;
private Report _reportToAdd;
DAO dao = new DAO();
public ObservableCollection<Report> Reports { get; set; }
public ObservableCollection<Locksmith> Locksmiths { get; set; }
public Report SelectedReport
{
get { return _selectedReport; }
set
{
_selectedLocksmith = Locksmiths.FirstOrDefault(x => value.Locksmith.Id == x.Id); ;
_selectedReport = value;
OnPropertyChanged("SelectedReport");
OnPropertyChanged("SelectedLocksmith");
}
}
public Locksmith SelectedLocksmith
{
get { return _selectedLocksmith; }
set
{
_selectedLocksmith = value;
OnPropertyChanged("SelectedLocksmith");
}
}
public Report ReportToAdd
{
get { return _reportToAdd;}
set
{
_reportToAdd = value;
OnPropertyChanged("ReportToAdd");
}
}
private RelayCommand addCommand;
public RelayCommand AddCommand
{
get
{
return addCommand ??
(addCommand = new RelayCommand(obj =>
{
Report report = (Report)obj;
dao.InsertReport(report);
Reports = dao.GetReports();
SelectedReport = report;
OnPropertyChanged("Reports");
}));
}
}
public DailyReportsModelView(Workshop workshop)
{
Reports = dao.GetReports();
Locksmiths = dao.GetLocksmiths(workshopId: workshop.Id);
ReportToAdd = new Report();
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged([CallerMemberName] string prop = "")
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(prop));
}
}
}
При попытке добавления Report в окне AddReportWindow, запись в бд происходит, однако обновление в ListBox в ManagerWindow нет
DataContext
, как окно открывается? Как передаются данные от одного окна в другое? В вопросе я просто вижу одну VM, но 2 окна, будто использующие эту одну VM, если это так (что странно), то опять-же, как задаетсяDataContext
...?