将多个列表绑定到WPF列表框

本文关键字:列表 WPF 绑定 | 更新日期: 2023-09-27 18:05:07

我有三个列表,从实体框架填充,我试图显示ItemType列表。列表框中的每个元素都应该像"TypeName CategoryName ItemTypeDetailName IsActive"。

Class ItemType has propertis TypeName(string), ItemTypeCategoryId (int), ItemTypeDetailId(int)
Class ItemTypeCategory has propertis ItemTypeCategoryId (int), CategoryName(string)
Class ItemTypeDetail has propertis ItemTypeDetailId (int), ItemTypeDetailName(string)

我尝试使用IValueConvertor。如何创建这个绑定?

将多个列表绑定到WPF列表框

你可以这样做:

视图模型:

public interface IItemType
{
    object Item1{ get; set; }
    object Item2{ get; set; }
    object Item3{ get; set; }
}
public class ItemType : IItemType
{
    public int ItemTypeCategoryId { get; set; }
    public int ItemTypeDetailId { get; set; }
    public string TypeName { get; set; }
    public object Item1
    {
        get
        {
            return ItemTypeCategoryId;
        }
        set
        {
            ItemTypeCategoryId = Convert.ToInt32(value);
        }
    }
    public object Item2
    {
        get
        {
            return ItemTypeDetailId;
        }
        set
        {
            ItemTypeDetailId = Convert.ToInt32(value);
        }
    }
    public object Item3
    {
        get
        {
            return TypeName;
        }
        set
        {
            TypeName = value.ToString();
        }
    }
}
public class ItemTypeCategory : IItemType
{
    public int ItemTypeCategoryId { get; set; }
    public string CategoryName { get; set; }
    public object Item1
    {
        get
        {
            return ItemTypeCategoryId;
        }
        set
        {
            ItemTypeCategoryId = Convert.ToInt32(value);
        }
    }
    public object Item2
    {
        get
        {
            return CategoryName;
        }
        set
        {
            CategoryName = value.ToString();
        }
    }
    public object Item3{ get; set; }
}
public class ItemTypeDetail : IItemType
{
    public int ItemTypeDetailId { get; set; }
    public string ItemTypeDetailName { get; set; }
    public object Item1
    {
        get
        {
            return ItemTypeDetailId;
        }
        set
        {
            ItemTypeDetailId = Convert.ToInt32(value);
        }
    }
    public object Item2
    {
        get
        {
            return ItemTypeDetailName;
        }
        set
        {
            ItemTypeDetailName = value.ToString();
        }
    }
    public object Item3 {get;set;}
}

主窗口:

    public List<IItemType> Items { get; set; }
    public MainWindow()
    {
        InitializeComponent();
        DataContext = this;
        IItemType i1 = new ItemType { ItemTypeCategoryId = 1, ItemTypeDetailId = 2, TypeName = "ItemType1" };
        IItemType i2 = new ItemTypeCategory { ItemTypeCategoryId = 2, CategoryName = "ItemTypeCategory1" };
        IItemType i3 = new ItemTypeDetail { ItemTypeDetailId = 3, ItemTypeDetailName = "ItemTypeDetail1" };
        Items = new List<IItemType>();
        Items.Add(i1);
        Items.Add(i2);
        Items.Add(i3);
    }
Xaml:

<Window>
    <ListBox  ItemsSource="{Binding Items}" >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <Grid>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="100"/>
                        <ColumnDefinition Width="100"/>
                        <ColumnDefinition Width="100"/>
                    </Grid.ColumnDefinitions>
                    <TextBlock Grid.Column="0" Text="{Binding Item1}" />
                    <TextBlock Grid.Column="1" Text="{Binding Item2}" />
                    <TextBlock Grid.Column="2" Text="{Binding Item3}" />
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Window>