我写了简单的代码
public ObservableCollection<string> Names …
public Window1()
{
PutInDataIntoNames();
InitializeComponent();
this.listBox1.ItemsSource = Names;
}
并在 xaml
<Grid>
<ListBox Margin="10,11,10,16"
Name="listBox1"
Background="Black"
Foreground="Orange"
/>
</Grid>
然后我想在 xaml 中设置 ItemsSource 属性。为了做到这一点,我写了以下内容:
ItemsSource="{Binding Path=Names}"
不幸的是,它不起作用。你能解释一下为什么以及如何做到这一点吗?