【问题标题】:can listbox show both key and value from dictionary列表框可以显示字典中的键和值吗
【发布时间】:2016-03-31 22:52:59
【问题描述】:

我有一本字典,MyClass 中有一个 int 属性 public Dictionary<MyClass, BitmapImage> MyList = new Dictionary<MyClass, BitmapImage>(); 我希望列表框显示此集合中的图像(如 ItemsSource?)并在每个图像附近的文本框中显示每个属性,这可能吗?

【问题讨论】:

  • 为什么是字典?只需将 BitmapImage 作为属性移动到 MyClass 中。
  • 是的。使用 ItemTemplate - 一个文本框和图像。
  • @M.kazemAkhgary 是的,这是一个更好的解决方案,但我没有弄清楚如何在 xaml 和代码中做到这一点,你介意帮助我吗?
  • 对不起,我不擅长 wpf 绑定。我只是觉得这样会更好。
  • @NickShepard 创建一个 List 以 BitmapImage 作为属性。

标签: c# wpf dictionary binding


【解决方案1】:

示例代码:

C#:

public MainWindow()
{
    InitializeComponent();
    PopulatePeople();
}

private void PopulatePeople()
{
   List<Person> persons = new List<Person>();
   for (int i = 0; i < 10; i++)
   {
      persons.Add(new Person() { Name = "Bob " + i.ToString(), ImageAddress = "Images/peach.jpg" });
    }
   listBox.ItemsSource = persons;
 }

XAML:

<ListBox Name="listBox">            
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <Image Source="{Binding Path=ImageAddress}" Width="50" Height="50"/>
                    <TextBox Text="{Binding Path=Name}" />
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>

MVVM 示例:

Download the example from OneDrive.

最好在ListBox中使用DataTemplate来显示ImageTextBox

型号:

public class Person 
{
    public int IDPerson { get; set; }
    public string Name { get; set; }
    public string ImageAddress { get; set; }
}

您可以在 ViewModel 的构造函数中填写您的集合:

视图模型:

public class YourViewModel:INotifyPropertyChanged 
{
    private ObservableCollection<Person> persons = new ObservableCollection<Person>();
    public ObservableCollection<Person> Persons
    {
       get { return persons; }
       set
       {
          persons = value;
          OnPropertyChanged("Persons");
        }
     }

     public  YourViewModel()
     {  
        FillThePersons();
    }

    private void FillThePersons()
    {           
        for (int i = 0; i < 10; i++)
        {
            persons.Add(new Person() { Name = "Bob " + i.ToString(),      
            ImageAddress="Images/peach.jpg" });// Images is the name folder in your project
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));

    }
} 

XAML:

<ListBox ItemsSource="{Binding Persons}">            
   <ListBox.ItemTemplate>
      <DataTemplate>
         <StackPanel Orientation="Horizontal">
            <Image Source="{Binding Path=ImageAddress}" Width="50" Height="50"/>
            <TextBox Text="{Binding Path=Name}" />
         </StackPanel>
      </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>

以下示例一张图片到所有项目:

<ListBox ItemsSource="{Binding Persons}">
   <ListBox.Resources>
      <BitmapImage x:Key="AnImage" UriSource="Images/yourImage.png" />
   </ListBox.Resources>
   <ListBox.ItemTemplate>
      <DataTemplate>
         <StackPanel Orientation="Horizontal">
            <Image Source="{StaticResource AnImage}" Width="50" Height="50"/>
            <TextBox Text="{Binding Path=Name}" />
         </StackPanel>
       </DataTemplate>
   </ListBox.ItemTemplate>
</ListBox>

结果:

【讨论】:

  • 谢谢,但我应该如何填写我的收藏?图像会有什么不同? @StepUp
  • 为什么总是出现异常 NullReferenceException? ' CollectionsVm.ProtectionCollection pr = new CollectionsVm.ProtectionCollection(); pr.ListHelmets.Add(new Helmet(){protection=7, Image="helmets.jpg"}); pr.ListHelmets.Add(new Helmet() { protection = 5, Image = "helmets.jpg" }); ''helmets'' 是一个包含少量图像的文件夹,对吗? "Images" 是您的代码@StepUp 中类似于 ImageAdress 的字符串
  • 现在我的列表框显示的东西真的很奇怪,它包含一个没有图像的项目和有点空的文本框@StepUp
  • 可以显示截图吗?我不能启动项目 unf 这就是我得到的 cs627722.vk.me/v627722797/2efaa/R8LDXDAQg1M.jpg @StepUp
  • 非常感谢您,如果可以的话,我会在您的答案中勾选十次!我将 ImageAdress 设置为 BitmapImage,它现在可以工作了。但是现在我遇到了另一个问题,当我只有项目而不是项目源时,拖放工作,但现在它没有@StepUp
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-17
  • 2020-06-04
  • 1970-01-01
  • 2021-09-01
  • 2022-01-15
相关资源
最近更新 更多