我有一个 ComboBox 标记为:
<ComboBox x:Name="TitleCombo"
Width="100"
Height="20"
DisplayMemberPath="TITLE_NAME"
SelectedValuePath="TITLE_CODEID"
DataContext="{Binding}"
ItemsSource="{Binding Path=Titles}" />
它是 上的唯一控件NoticeWindow.xaml
,它具有以下代码隐藏:
public partial class NoticeWindow : Window
{
private readonly Xt900Context _dbContext = new Xt900Context();
public NoticeWindow()
{
InitializeComponent();
var tits = _dbContext.TITLEs.ToList();
Titles = new ObservableCollection<TITLE>(tits);
DataContext = this;
TitleCombo.ItemsSource = Titles;
}
ObservableCollection<TITLE> Titles { get; set; }
}
如果没有该TitleCombo.ItemsSource = Titles
语句,ComboBox 将保持空白。为什么是这样?