C# 列表框能否同时显示字典中的键和值

C# 列表框能否同时显示字典中的键和值,c#,wpf,dictionary,binding,C#,Wpf,Dictionary,Binding,我有一本字典,我的类中有一个int属性 public Dictionary MyList=new Dictionary() 我希望listbox显示此集合中的图像(如ItemsSource?),并在每个图像附近的文本框中显示每个属性,这可能吗?代码隐藏示例: public MainWindow() { InitializeComponent(); PopulatePeople(); } private void PopulatePeople() { List<Per

我有一本字典,我的类中有一个int属性
public Dictionary MyList=new Dictionary()

我希望listbox显示此集合中的图像(如ItemsSource?),并在每个图像附近的文本框中显示每个属性,这可能吗?

代码隐藏示例:

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;
 }
<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>
public class Person 
{
    public int IDPerson { get; set; }
    public string Name { get; set; }
    public string ImageAddress { get; set; }
}
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));

    }
} 
<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>
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;
 }
<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>
public class Person 
{
    public int IDPerson { get; set; }
    public string Name { get; set; }
    public string ImageAddress { get; set; }
}
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));

    }
} 
<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>
您可以在ViewModel的构造函数中填充集合:

视图模型:

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;
 }
<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>
public class Person 
{
    public int IDPerson { get; set; }
    public string Name { get; set; }
    public string ImageAddress { get; set; }
}
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));

    }
} 
<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>
public类YourViewModel:INotifyPropertyChanged
{
私人可观察收集人员=新可观察收集();
公众人士
{
获取{返回人员;}
设置
{
人=价值;
财产变更(“人员”);
}
}
公共视图模型()
{  
填充人物();
}
私人空白填充人()
{           
对于(int i=0;i<10;i++)
{
添加(new Person(){Name=“Bob”+i.ToString(),
ImageAddress=“Images/peach.jpg”});//Images是项目中的名称文件夹
}
}
公共事件属性更改事件处理程序属性更改;
公共void OnPropertyChanged(字符串propertyName)
{
if(PropertyChanged!=null)
PropertyChanged(这是新的PropertyChangedEventArgs(propertyName));
}
} 
XAML:

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;
 }
<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>
public class Person 
{
    public int IDPerson { get; set; }
    public string Name { get; set; }
    public string ImageAddress { get; set; }
}
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));

    }
} 
<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>

结果是:


为什么要查字典?只需将BitmapImage作为属性移动到MyClass中即可。是。使用ItemTemplate-一个文本框和图像。@M.kazemAkhgary是的,这是一个更好的解决方案,但我没有设法找出如何在xaml和代码中实现它,你介意帮助我吗?对不起,我不擅长wpf绑定。我只是觉得这样会更好。@NickShepard创建一个以BitmapImage为属性的列表。谢谢,但我应该如何填充我的集合?图像会有什么不同@为什么我总是有一个异常NullReferenceException?”CollectionsVm.ProtectionCollection pr=新的CollectionsVm.ProtectionCollection();添加(新头盔(){protection=7,Image=“helmets.jpg”});添加(新头盔(){protection=5,Image=“helmets.jpg”});“”“头盔”是一个包含很少图像的文件夹,对吗?“Images”在你的代码@stepup中是字符串状的imageAddress现在我的列表框显示的是smth真的很奇怪,它由一个没有图像的项目和一个空的文本框组成@stepup你能显示屏幕截图吗?我不能启动项目unf这就是我得到的@steppup非常感谢你,如果可以的话,我会把你的答案打上十几次正确的记号!我将ImageAddress设置为BitmapImage,现在可以使用了。但现在我又遇到了另一个问题,当我只有项目,而不是项目资源时,拖放就起作用了,但现在它并没有“升级”