c# - 列表框可以显示字典中的键和值

标签 c# wpf dictionary binding

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

最佳答案

代码隐藏示例:

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 的构造函数中填充集合:

View 模型:

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>

结果:

enter image description here

关于c# - 列表框可以显示字典中的键和值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34470661/

相关文章:

c# - .Net 中的响应式(Reactive)扩展 (C#) - Subject<T> 实例只处理一个订阅

c# - Uri.UnescapeDataString 在不同的计算机上失败

c# - 选择多个文件和文件夹所需的对话框 .NET

c# - 如何访问或修改动态创建的按钮

c# - 第三方 WPF 控件 : Devexpress vs Telerik

fluent-nhibernate - 如何在 Fluent NHibernate 中映射 IDictionary<string, object>?

c# - 将对象映射到字典,反之亦然

c# - 文本框控件中的验证问题

c# - 文本未在 ListViewItem 中换行

python - 从字典创建数据帧,键不作为索引