使用ObservableCollection填充listview时出现问题

时间:2011-07-26 14:08:34

标签: c# wpf listview

当我按下按钮列表视图不填充。任何人都可以帮助我吗?  public ObservableCollection CheckInCollection

    public ObservableCollection<CheckInData> CheckInCollection
    {
        get { return _CheckInCollection; }
    }

    public class CheckInData
    {
        public string RoomNumber { get; set; }
        public decimal Price { get; set; }
        public string Currecny { get; set; }
        public decimal Discount { get; set; }
        public string CheckOut { get; set; }
        public int TotalDay { get; set; }
        public decimal TotalPrice { get; set; }
        public int CheckOutYear { get; set; }
        public int CheckOutMonth { get; set; }
        public int CheckOutDay { get; set; }
        public Boolean IncToday { get; set; }
    }
    private void btnAdd_Click(object sender, RoutedEventArgs e)
    {
        string _CheckOut = "" + datePicker1.SelectedDate.Value.Day.ToString() + "/" + datePicker1.SelectedDate.Value.Month.ToString() + "/" + datePicker1.SelectedDate.Value.Year.ToString() + "";
        int _TotalDays;
        decimal _TotalPrice;
        Boolean _IncToday;

        if (cbIncToday.IsChecked == true)
        {
            _IncToday = true;
            var checkIn = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
            var checkOut = new DateTime(datePicker1.SelectedDate.Value.Year, datePicker1.SelectedDate.Value.Month, datePicker1.SelectedDate.Value.Day + 1);

            TimeSpan ts = checkOut - checkIn;
            _TotalDays = Convert.ToInt32(ts.Days);
            _TotalPrice = _TotalDays * Convert.ToDecimal(txtPrice.Text);
        }
        else
        {
            _IncToday = false;
            var checkIn = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
            var checkOut = new DateTime(datePicker1.SelectedDate.Value.Year, datePicker1.SelectedDate.Value.Month, datePicker1.SelectedDate.Value.Day);

            TimeSpan ts = checkOut - checkIn;
            _TotalDays = Convert.ToInt32(ts.Days);
            _TotalPrice = _TotalDays * Convert.ToDecimal(txtPrice.Text);
        }

        _CheckInCollection.Add(new CheckInData
        {
            RoomNumber = cbRooms.Text,
            Price = Convert.ToDecimal(txtPrice.Text),
            Currecny = cbCurreny.Text,
            Discount = Convert.ToDecimal(txtDiscount.Text),
            CheckOut = _CheckOut,
            CheckOutYear = datePicker1.SelectedDate.Value.Year,
            CheckOutMonth = datePicker1.SelectedDate.Value.Month,
            CheckOutDay = datePicker1.SelectedDate.Value.Day,
            IncToday = _IncToday,
            TotalDay = _TotalDays,
            TotalPrice = _TotalPrice

        });
    }

XAML:

Title="CheckIn" Height="473" Width="682" WindowStartupLocation="CenterScreen" ResizeMode="NoResize" DataContext="{Binding RelativeSource={RelativeSource Self}}">
<ListView Margin="8,8,5,1.04" DisplayMemberPath="{Binding CheckInCollection}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Room Number" Width="85" DisplayMemberBinding="{Binding RoomNumber}"/>
                    <GridViewColumn Header="Price" Width="60" DisplayMemberBinding="{Binding Price}"/>
                    <GridViewColumn Header="Currecny" Width="70" DisplayMemberBinding="{Binding Currency}"/>
                    <GridViewColumn Header="Discount" Width="65" DisplayMemberBinding="{Binding Discount}"/>
                    <GridViewColumn Header="Check Out" Width="100" DisplayMemberBinding="{Binding CheckOut}"/>
                    <GridViewColumn Header="Total Days" Width="100" DisplayMemberBinding="{Binding TotalDays}"/>
                    <GridViewColumn Header="Total Price" Width="100" DisplayMemberBinding="{Binding TotalPrice}"/>
                </GridView>
            </ListView.View>
        </ListView>

2 个答案:

答案 0 :(得分:1)

试试这个

<ListView Margin="8,8,5,1.04" ItemsSource="{Binding Path=CheckInCollection UpdateSourceTrigger = PropertyChanged}">

答案 1 :(得分:1)

在ListView元素上,您应该将ItemsSource属性绑定到您的集合,而不是DisplayMemberPath。