我有以下观点:
<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:ClassLibrary1;assembly=ClassLibrary1"
d:DataContext="{d:DesignInstance Type=local:DailyReportsModelView}"
mc:Ignorable="d"
Title="ManagerWindow" Height="450" Width="800">
<Grid>
<TabControl>
<TabItem Header="Отчёты">
<Grid Background="#FFE5E5E5">
<ListBox ItemsSource="{Binding Reports}" SelectedItem ="{Binding SelectedReport}" Margin="0,0,397,0">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Margin="5">
<TextBlock Text="{Binding Path=Locksmith}" />
<TextBlock Text="{Binding Path=Date}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<StackPanel Grid.Column ="1" Background="#FFE5E5E5" DataContext ="{Binding SelectedReport}" Margin="397,0,0,0">
<TextBlock Text="Выбранный элемент" />
<TextBlock Text="Количество деталей" />
<TextBox Text="{Binding DetailsCount, UpdateSourceTrigger=PropertyChanged}"/>
<TextBlock Text="Дата" />
<TextBox Text="{Binding Date, UpdateSourceTrigger=PropertyChanged}" />
<TextBlock Text="Работник" />
</StackPanel>
<ComboBox ItemsSource="{Binding Locksmiths}" SelectedItem="{Binding SelectedLocksmith}" Margin="402,104,0,258"/>
</Grid>
</TabItem>
</TabControl>
</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 ClassLibrary1
{
public class DailyReportsModelView : INotifyPropertyChanged
{
private Report _selectedReport;
private Locksmith _selectedLocksmith;
public ObservableCollection<Report> Reports { get; set; }
public ObservableCollection<Locksmith> Locksmiths { get; set; }
public Report SelectedReport
{
get { return _selectedReport; }
set
{
_selectedLocksmith = value.Locksmith;
_selectedReport = value;
OnPropertyChanged("SelectedLocksmith");
OnPropertyChanged("SelectedReport");
}
}
public Locksmith SelectedLocksmith
{
get { return _selectedLocksmith; }
set
{
_selectedLocksmith = value;
OnPropertyChanged("SelectedLocksmith");
}
}
public DailyReportsModelView(Workshop workshop)
{
DAO dao = new DAO();
Reports = dao.GetReports();
Locksmiths = dao.GetLocksmiths(workshopId: workshop.Id);
}
public event PropertyChangedEventHandler PropertyChanged;
public void OnPropertyChanged([CallerMemberName] string prop = "")
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(prop));
}
}
}
当您在ListBox中选择一个元素时,StackPanel的字段与SelectedReport的DataContext发生更改,但ComboBox不响应这些更改,这是什么问题?
您不应分配与 setter 中的当前属性无关的字段。
它
value.Locksmith
必须是集合的一部分Locksmiths
。如果您单独拥有此对象,则需要从集合中选择该对象,例如如下所示:
您
ComboBox
有义务SelectedLocksmith
,并且当有更改时,SelectedReport
仅通知更改SelectedReport
。添加OnPropertyChanged("SelectedLocksmith");
要设置的属性SelectedReport
。