【发布时间】:2015-04-07 01:26:27
【问题描述】:
我是 WPF 的初学者,正在尝试将 ComboBox 的项目绑定到 ObservableCollection
我使用了这个代码:
XAML
<Window x:Class="comboBinding2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
Title="MainWindow" Height="350" Width="525">
<Grid>
<ComboBox x:Name="cmbTest" ItemsSource="{Binding Path=cmbContent}" Width="200" VerticalAlignment="Center" HorizontalAlignment="Center" />
</Grid>
</Window>
C#
public MainWindow()
{
cmbTest.ItemsSource = cmbContent;
cmbContent.Add("test 1");
cmbContent.Add("test 2");
InitializeComponent();
}
public ObservableCollection<string> cmbContent { get; set; }
在我尝试调试之前,我在此代码上没有收到任何错误,它会引发错误:
TargetInvocationError
PresentationFramework.dll 中出现“System.Reflection.TargetInvocationException”类型的未处理异常
谁能告诉我我做错了什么?
【问题讨论】:
标签: c# wpf xaml binding combobox