Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将ListBox数据绑定到包含可观察集合的类的列表_C#_Wpf_Silverlight - Fatal编程技术网

C# 将ListBox数据绑定到包含可观察集合的类的列表

C# 将ListBox数据绑定到包含可观察集合的类的列表,c#,wpf,silverlight,C#,Wpf,Silverlight,我有一个自定义类的项目列表。该类包含另一个类的可观察集合,该类有两个字符串值。我想根据另一个字符串值将数据绑定到其中一个字符串值。作为一个虚构的例子: public class Person { public string Name { get; set; } public ObservableCollection<Pet> Pets { get; set; } } public class Pet { public string AnimalType { g

我有一个自定义类的项目列表。该类包含另一个类的可观察集合,该类有两个字符串值。我想根据另一个字符串值将数据绑定到其中一个字符串值。作为一个虚构的例子:

public class Person
{
    public string Name { get; set; }
    public ObservableCollection<Pet> Pets { get; set; }
}
public class Pet
{
    public string AnimalType { get; set; }
    public string Name { get; set; }
}
公共类人物
{
公共字符串名称{get;set;}
公共可观测集合{get;set;}
}
公营宠物
{
公共字符串AnimalType{get;set;}
公共字符串名称{get;set;}
}
然后,我将列表框绑定到人员列表:

List<Person> people = new List<Person>();
Person p = new Person() { Name = "Joe", Pets = new ObservableCollection<Pet>() { new Pet() { Name = "Spot", AnimalType = "Dog" }, new Pet() { Name = "Whiskers", AnimalType = "Cat" } } };
people.Add(p);
p = new Person() { Name = "Jim", Pets = new ObservableCollection<Pet>() { new Pet() { Name = "Juniper", AnimalType = "Cat" }, new Pet() { Name = "Butch", AnimalType = "Dog" } } };
people.Add(p);
p = new Person() { Name = "Jane", Pets = new ObservableCollection<Pet>() { new Pet() { Name = "Tiny", AnimalType = "Dog" }, new Pet() { Name = "Tulip", AnimalType = "Cat" } } };
people.Add(p);
MyListBox.ItemsSource = people;
List people=newlist();
Person p=new Person(){Name=“Joe”,Pets=new observeCollection(){new Pet(){Name=“Spot”,AnimalType=“Dog”},new Pet(){Name=“胡须”,AnimalType=“Cat”}};
新增(p);
p=newperson(){Name=“Jim”,Pets=newobserveCollection(){newpet(){Name=“Juniper”,AnimalType=“Cat”},newpet(){Name=“Butch”,AnimalType=“Dog”}};
新增(p);
p=newperson(){Name=“Jane”,Pets=newobserveCollection(){newpet(){Name=“Tiny”,AnimalType=“Dog”},newpet(){Name=“Tulip”,AnimalType=“Cat”}};
新增(p);
MyListBox.ItemsSource=人;
如果动物类型是狗,我想绑定人名和宠物名。我知道我可以使用索引器绑定,但我特别需要狗条目,即使它是宠物收藏中的第二个条目。下面的XAML用于显示集合中的第一项,但对于列表中的第二项,它是错误的,因为狗是集合中的第二项:

        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Height="55.015" Width="302.996">
                    <TextBlock TextWrapping="Wrap" Text="{Binding Name}" Height="25.015" VerticalAlignment="Top" Margin="0,0,8,0"/>
                    <TextBlock Text="{Binding Pets[0].Name}"></TextBlock>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>


有人能为我提供一些指导吗?

使用值转换器仅显示狗

XAML:


代码隐藏:

public class FindDogConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        IEnumerable<Pet> pets = value as IEnumerable<Pet>;
        return pets.Single(p => p.AnimalType == "Dog").Name;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
公共类FindDogConverter:IValueConverter
{
公共对象转换(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
IEnumerable=作为IEnumerable的值;
返回宠物。单个(p=>p.AnimalType==“狗”)。名称;
}
公共对象转换回(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
抛出新的NotImplementedException();
}
}

谢谢。看起来我可以使用converter参数来保持通用性,所以这正是我所需要的。
public class FindDogConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        IEnumerable<Pet> pets = value as IEnumerable<Pet>;
        return pets.Single(p => p.AnimalType == "Dog").Name;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}