wpf

时间:2016-09-13 15:01:38

标签: c# wpf

我有书籍和ListVew的集合,用于表示书籍数据。我正在尝试制作文本块列表。每个文本块都需要使用转换器转换数据,转换器采用一个模型并为一个字符串提供数据,但我不知道如何绑定集合并将一个模型传递给转换器。

书籍型号:

namespace Books.Models
{
    public class Book : IModel
    {
        public Book()
        {
            Authors = new List<Author>();
            Tags = new List<string>();
        }
        public string Name { get; set; }
        public List<Author> Authors { get; set; }
        public string ISBN { get; set; }
        public int Pages { get; set; }
        public List<string> Tags { get; set; }
        public int PublicationYear { get; set; }
        public House House { get; set; }
    }
}

书籍收藏:

namespace Books.Collections
{
    public class ModelsCollection : Dictionary<string, IModel>
    {
        public void Add(IModel model)
        {
            Add(model.Name, model);
        }
        public IEnumerable<IModel> GetAll()
        {
            foreach (IModel model in Values)
                yield return model;   
        }
    }
}

转换器:

namespace Books
{

    [ValueConversion(typeof(ModelsCollection), typeof(string))]
    public class BooksConvertor : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            Book book = (Book)value;
            return book.Name + " - " + string.Join(", ", book.Authors.Select(x => x.Name));
        }

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

XAML中的ListView

    <ListView >
        <TextBlock Text="{Binding Path=Book,Converter={StaticResource BooksConvertor}}"/>
    </ListView>

1 个答案:

答案 0 :(得分:1)

首先,您需要一种方法来设置您正在使用的窗口/视图的DataContext。我创建了一个简单的ViewModel(使用你的模型)。

public class ViewModel
{
    public List<IModel> Books { get; private set; }

    public ViewModel()
    {
        var modelCollection = new ModelsCollection();

        for (var i = 0; i < 10; i++)
        {
            var testBook = new Book
            {
                Name = "Test Book " + i
            };
            testBook.Authors.Add(new Author
            {
                Name = "Test Author " + i
            });

            modelCollection.Add(testBook);;
        }

        Books = modelCollection.GetAll();
    }
}

Ctor代码隐藏的Window中,只需执行以下操作:

DataContext = new ViewModel();

接下来,您的ListView需要绑定到ViewModel中指定的图书集合,并且还有一个预定义的模板可以正确绑定到每个图书对象。您需要将ListView更新为:

<ListView ItemsSource="{Binding Books}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding ., Converter={StaticResource BooksConvertor}}"/>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

最后,您需要更新转换器以使用IModel类型,因为这是传入的数据。

[ValueConversion(typeof(IModel), typeof(string))]
public class BooksConvertor : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        var book = value as Book;
        if (book != null)
        {
            return book.Name + " - " + string.Join(", ", book.Authors.Select(x => x.Name));
        }
        else
        {
            return "";
        }
    }

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