私はこのコンボボックスを持っています
<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"));
}
}
私はこのxamlの「魔法」にかなり慣れていないので、データバインディングがどのように機能するかを正しく理解していないと思います. ItemsSource="{Binding WonderList}"
(コードビハインドから)その名前のコレクションを取得し、それらの名前とパスを表示する必要があると思いますが、空のリストを示します。
コード ビハインドで行う場合Combo1.ItemsSource = WonderList;
(xaml を使用し、コード ビハインドを回避することを好みます)、2 つの空白スロットが表示されますが、項目を表示する方法がわかりません。
正しい方向に私を向けることができますか?
ありがとう