将多个列表绑定到WPF ListBox

时间:2015-07-27 11:01:38

标签: c# wpf

我有三个列表,从实体框架填充,我试图显示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。 如何创建此绑定?

1 个答案:

答案 0 :(得分:0)

你可以这样做:

的ViewModels:

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>