我有一个简单的组合框,里面有一个复选框:
<ComboBox Height="23" HorizontalAlignment="Left" Margin="158,180,0,0" Name="comboBox1" VerticalAlignment="Top" Width="120" ItemsSource="{Binding collection}">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<CheckBox Content="{Binding Name}"></CheckBox>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
datacontext 只是背后的代码,为了测试它,我使用以下代码:
public ObservableCollection<Foo> collection { get; set; }
private void button1_Click(object sender, RoutedEventArgs e)
{
collection = new ObservableCollection<Foo>();
this.comboBox1.ItemsSource = collection;
Foo f = new Foo("DSD");
collection.Add(f);
}
当我在代码中设置 ItemsSource 时,它工作正常,但我想在 Xaml 中设置 ItemsSource,但是使用上面的 Xaml 不起作用。我也尝试将其设置为 Path = ""。有人知道为什么吗?
谢谢