English 中文(简体)
与含有可观察收藏的分类列表中包含数据绑定的列表框
原标题:Databinding ListBox to a List of a Class that Contains an Observable Collection

我有一个自定义类的项目列表。 该类包含一个可观测到的另一类的收藏, 包含两个字符串值。 我要根据另一个字符串的某个值将数据绑绑到一个字符串值中。 因此, 一个虚构的例子 :

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

然后把列表框绑在个人名单上:

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;

如果动物类型是 Dog, 我想绑定一个人的名字和宠物的名字。 我知道可以使用索引器绑定它, 但我特别需要狗的条目, 即使它是宠物收藏中的第二个条目。 下面的 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: 时间轴:

<TextBlock Text="{Binding Pets, Converter={StaticResource FindDogConverter}}" />

后面的代码 :

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();
    }
}
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签