数据绑定不起作用,看不到任何错误

时间:2016-12-01 16:26:27

标签: c# wpf xaml

我正在跟踪msdn(https://msdn.microsoft.com/en-us/library/ms752347(v=vs.110).aspx)的WPF教程。我没有收到任何错误但也没有输出。 我希望从代码中指定的属性中查看ListBox中的数据。我正在粘贴下面的xaml和cs文件。在这方面的任何帮助表示赞赏。如果有人可以将我推荐给除了msdn之外的WPF上的一些教程,我将不胜感激。

MainWindow.xaml

<Window x:Class="Practice_1.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:sys="clr-namespace:System;assembly=mscorlib"
    xmlns:local="clr-namespace:Practice_1"
    mc:Ignorable="d"
    Title="MainWindow" Height="150" Width="300">
<Window.Resources>

</Window.Resources>

<ListBox Width="Auto" Height="Auto">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock Text="{Binding Path=Description}"/>
                <TextBlock Text="{Binding Path=StartPrice}"/>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>

</ListBox>

MainWindow.xaml.cs

    namespace Practice_1
{

    public partial class MainWindow : Window
    {
        public List<AuctionItem> AuctionItemObjects;

        public MainWindow()
        {
            AuctionItemObjects = new List<AuctionItem>()
            {
                new AuctionItem()
                {
                    Description = "Inside C#. second edition",
                    StartDate = "8/8/2016",
                    StartPrice = "1000",
                    Category = "Books",
                    OwnerName = "Chummi",
                    MemberSince = "2004",
                    OwnerRating = "15",
                    SpecialFeatures = "Color",
                    CurrentPrice = "10"

                },
                new AuctionItem()
                {
                    Description = "Laptop - only 1 year old",
                    StartDate = "8/9/2016",
                    StartPrice = "100",
                    Category = "Electronics",
                    OwnerName = "Mark",
                    MemberSince = "2005",
                    OwnerRating = "10",
                    SpecialFeatures = "Highlight"
                },
                new AuctionItem()
                {
                    Description = "TV Drama Series",
                    StartDate = "8/10/2016",
                    StartPrice = "400",
                    Category = "DVDs",
                    OwnerName = "Chuhaan",
                    MemberSince = "2006",
                    OwnerRating = "5",
                    SpecialFeatures = ""
                },
                new AuctionItem()
                {
                    Description = "My DVD Collection",
                    StartDate = "8/11/2016",
                    StartPrice = "5000",
                    Category = "DVDs",
                    OwnerName = "Charsi",
                    MemberSince = "2008",
                    OwnerRating = "35",
                    SpecialFeatures = "Highlight"
                },

            };
            InitializeComponent();
            DataContext = this;
        }

        public class AuctionItem
        {

            public string Description { get; set; }

            public string StartPrice { get; set; }

            public string StartDate { get; set; }

            public string Category { get; set; }

            public string SpecialFeatures { get; set; }

            public string OwnerName { get; set; }

            public string OwnerRating { get; set; }

            public string MemberSince { get; set; }

            public string CurrentPrice { get; set; }
        }

    }

}

1 个答案:

答案 0 :(得分:1)

您需要将ListBox绑定到您的收藏夹。

类似的东西:

<ListBox Width="Auto" Height="Auto" ItemsSource="{Binding AuctionItemObjects}">

否则,您如何知道自己想要的ListBox

请注意以下几点:

  • 通常情况下,拥有模型视图模型会更好。视图模型将是视图DataContext(这是您的窗口)。这是MVVM模式,你应该学习它。
  • 当你从基础列表中添加或删除项目时,List<T>无法更新ListBox。这是因为它没有实现INotifyCollectionChanged,如果您需要,请考虑ObservableCollection<T>
  • AuctionItem中的任何属性都不会在更改时更新用户界面,因为您没有实施INotifyPropertyChanged
  • AuctionItemObjects需要是属性,而不是字段。 WPF不会绑定字段,因此public List<AuctionItem> AuctionItemObjects {get; set; }