WPF列表绑定失败

时间:2017-05-07 23:04:35

标签: c# wpf xaml

我已按照here的指示行事。然而,我的绑定似乎失败了,因为我的窗口没有结果。我想要做的只是将项目列表绑定到ListView。 XAML文件内容:

<Window x:Class="IV_sem___PwSG___WPF_task1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:local="clr-namespace:IV_sem___PwSG___WPF_task1"
    mc:Ignorable="d"
    Title="MainWindow" Height="600" Width="800"
    MinHeight="500" MinWidth="500">
<Window.Resources>
    <DataTemplate x:Key="MyItemTemplate">
        <Grid Margin="5" Background="Aqua">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="Auto"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"/>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="Auto"/>
            </Grid.ColumnDefinitions>
            <TextBlock Text="{Binding Name}" Grid.Column="0" Grid.Row="0"
                       FontSize="18" FontFamily="Arial"></TextBlock>
            <TextBlock Text="{Binding Description}" Grid.Column="0" Grid.Row="1"
                       FontSize="14" FontFamily="Arial"></TextBlock>
            <TextBlock Text="{Binding Category}" Grid.Column="0" Grid.Row="2"
                       FontSize="12" FontFamily="Arial"></TextBlock>
            <TextBlock Text="{Binding Price}" Grid.Column="1" Grid.RowSpan="1"></TextBlock>
            <Button Content="Add to cart" Grid.Column="2" Grid.Row="1"/>
        </Grid>
    </DataTemplate>
</Window.Resources>
<!--<Window.DataContext>
    <local:MainWindow />
</Window.DataContext>-->

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>

    <Menu Grid.Column="0" Grid.Row="0">
        <MenuItem Header="File">
            <MenuItem Header="Load"/>
            <MenuItem Header="Save"/>
            <Separator/>
            <MenuItem Header="Exit" Click="ExitMenuItem_Click"/>
        </MenuItem>
        <MenuItem Header="Products">
            <MenuItem Header="Add products" Click="AddProductMenuItem_Click"/>
            <MenuItem Header="Clear products"/>
        </MenuItem>
        <MenuItem Header="About" Click="AboutMenuItem_Click"/>
    </Menu>

    <TabControl Grid.Column="0" Grid.Row="1">
        <TabItem Header="Shop">
            <!--<Grid>-->
            <ListView ItemsSource="{Binding itemList}"
                      ItemTemplate="{StaticResource MyItemTemplate}"/>
            <!--</Grid>-->
        </TabItem>
        <TabItem Header="Warehouse">

        </TabItem>
    </TabControl>
</Grid>

.cs文件内容:

namespace IV_sem___PwSG___WPF_task1{
public partial class MainWindow : Window
{
    public List<Item> itemList { get; set; }

    public MainWindow()
    {
        itemList = new List<Item>();
        InitializeComponent();
    }

    private void AboutMenuItem_Click(object sender, RoutedEventArgs e)
    {
        MessageBox.Show("This is simple shop manager.", "About application", MessageBoxButton.OK, MessageBoxImage.Information);
    }

    private void ExitMenuItem_Click(object sender, RoutedEventArgs e)
    {
        this.Close();
    }

    public class Item
    {
        string Name { get; set; }
        string Description { get; set; }
        Category Category { get; set; }
        double Price { get; set; }
        public Item(string a, string b, Category c, double d)
        {
            Name = a; Description = b; Category = c; Price = d;
        }

    }

    private void AddProductMenuItem_Click(object sender, RoutedEventArgs e)
    {
        itemList.Add(new Item("Computer", "Computer's description", Category.Electronics, 2499.99));
        itemList.Add(new Item("Apple", "Apple's description", Category.Food, 1.99));
        itemList.Add(new Item("Computer", "Computer's description", Category.Electronics, 2499.99));
    }
}

public enum Category
{
    Electronics, Food
}
}

类别是枚举类型。 List是MainWindow类的属性。 它应该按照上面的链接工作。

1 个答案:

答案 0 :(得分:1)

使用Observable集合代替List,如下所示

public ObservableCollection<Item> itemList { get; set; }

因为Observable Collection使用INotifyCollectionChanged实现,INotifyPropertyChanged它通知集合更改事件并更新UI。