我有这样的组合框绑定集合,组合框
<ComboBox Height="30" SelectedIndex="0" Margin="5 3 5 3" Width="170" ItemsSource="{Binding WonderList}" SelectedValuePath="selectedWonder">
<ComboBox.ItemTemplate>
<DataTemplate>
<WrapPanel>
<Image Source="{Binding Path}" Height="20"></Image>
<Label Content="{Binding Name}" Style="{StaticResource LabelComboItem}"></Label>
</WrapPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
,我想展示的项目,如图像以及文本。
这是在项目列表
public class Wonder: INotifyPropertyChanged
{
private string name;
private string path;
public event PropertyChangedEventHandler PropertyChanged;
#region properties, getters and setters
public String Name { get; set; }
public String Path { get; set; }
#endregion
public Wonder(string name, string path)
{
this.name = name;
this.path = path;
}
protected void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
}
对象和窗口
public class Window1 {
public List<Wonder> WonderList;
public Window1()
{
InitializeComponent();
WonderList = new List<Wonder>();
WonderList.Add(new Wonder("Alexandria", "Resources/Images/Scans/Wonders/Alexandria.jpg"));
WonderList.Add(new Wonder("Babylon", "Resources/Images/Scans/Wonders/Babylon.jpg"));
}
}
I'm很新,此XAML“神奇”的后面的代码公务舱和猜测我不明白数据绑定是如何工作的,我认为使用ItemsSource="{Binding WonderList}"
它应该使用该名称的集合(来自代码背后)并显示它们的名称和路径,但它显示一个空列表。
如果我在后面的代码中使用Combo1.ItemsSource = WonderList;
(我更喜欢使用xaml并避免后面的代码),它显示两个空白插槽,但仍不知道如何显示这些项目。
您能否指点我正确的方向?
谢谢