Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Listivew with binding not updating

$
0
0

I've got a listview with binding and it's not updating. Can somebody find the bug? Wish I had some money, because I would offer a reward.

In this screen cap, the window on the right (Active Dinosaur List) is NOT updating when the status of a particular dinosaur is changing (note that when you click on the dinosaur (in this case, Nancy) it shows, correctly, that her status is, "Moving to food" while the Active Dinosaur List is showing her still Resting:

Here's all the relevant code, starting with the XAML for the window:

<Window x:Class="DinosaurIsland.ActiveDinosaurList"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="clr-namespace:DinosaurIsland" 

        Title="ActiveDinosaurList" Height="850" Width="245" WindowStyle="SingleBorderWindow"  Icon="/DinosaurIsland;component/Icon1.ico"  ResizeMode="NoResize"   ><Window.Resources><local:EnergyBarColorConverter x:Key="EnergyBarColorConverter"/><local:DinoStatusConverter x:Key="DinoStatusConverter"/><DataTemplate x:Key="DinosaurInfo"><StackPanel Orientation="Vertical" ><Label Name="DinosaurName"  Margin="0,0,0,-8" Content="{Binding Path=PersonalName}"/><Label Name="DinosaurSpecies" Margin="0,0,0,-8" FontStyle="Italic" Content="{Binding Path=Specie}"/><Label Name="DinosaurStatus" Margin="0,0,0,-8" Content="{Binding Path=State, Converter={StaticResource DinoStatusConverter}}"/><Label  HorizontalAlignment="Center" Margin="0,0,0,-2" Content="Energy" /><ProgressBar  Name="Health" Margin="0,0,0,10" HorizontalAlignment="Center" VerticalAlignment="Top" Width="160" Height="15"  
                              Foreground ="{Binding Path=Health, Converter={StaticResource EnergyBarColorConverter}}"  Value="{Binding Path=Health}"  /><Separator/></StackPanel></DataTemplate></Window.Resources><Grid Width="210"><ListView x:Name="DinoListView"  Width="207" ItemsSource="{Binding Path=Dinosaurs}" HorizontalAlignment="Left" Margin="3,0,0,0"><ListView.View><GridView><GridViewColumn Width="180" Header="Select Dinosaur"  CellTemplate="{StaticResource DinosaurInfo}" /></GridView></ListView.View></ListView></Grid></Window>

Here's the Dinosaur class:

using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;

namespace DinosaurIsland 
{
    public class Dinosaur : INotifyPropertyChanged
    {
        public string _specie;
        public string Specie
            {
            get { return _specie; }
            set{_specie = value; RaisePropertyChanged("Specie");}
            }
        public int Age { get; set; }
        public int Weight { get; set; }
        public double Height { get; set; }
        public int _health;
        public int Health 
            {
            get { return _health; }
            set{_health = value; RaisePropertyChanged("Health");}
            }
        public double Water { get; set; }
        public double FoodConsumed { get; set; }
        public bool Sex { get; set; }
        public string PersonalName { get; set; }
        public System.Windows.Point Head = new System.Windows.Point();
        public List<System.Windows.Point> Location { get; set; }
        public double Length { get; set; }
        public double Speed { get; set; }
        public byte _state;
        public byte State             
            {
            get { return _state; }
            set{_state = value; RaisePropertyChanged("State");}
            }
        public System.Windows.Point Goal = new System.Windows.Point();

        public System.Windows.Point[] FoodLocation = new System.Windows.Point[5]; // The last five locations that the dino found food
        public System.Windows.Point[] WaterLocation = new System.Windows.Point[5]; // The last five locations that the dino found water

        // Constructor
        public Dinosaur()
        {
           
        }

        public event PropertyChangedEventHandler PropertyChanged;
        //called when a property is changed
        protected void RaisePropertyChanged(string PropertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
            }
        }
    }
}

Here's the ViewModel class:

using System;
using System.Collections.Generic;
using System.Collections;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;

namespace DinosaurIsland
{
    public class ViewModel : INotifyPropertyChanged
    {
        public ViewModel()
        {
           this.Dinosaurs = new ObservableCollection<Dinosaur>();
           for(int i = 0; i < MainWindow.Dinosaurs.Count; i++)

               this.Dinosaurs.Add(new Dinosaur()
            {
                PersonalName = MainWindow.Dinosaurs[i].PersonalName,
                Specie = MainWindow.Dinosaurs[i].Specie,
                Health =  MainWindow.Dinosaurs[i].Health,
                State =  MainWindow.Dinosaurs[i].State
            });
        }

        public event PropertyChangedEventHandler PropertyChanged;
        //called when a property is changed
        public void RaisePropertyChanged(string PropertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
            }
        }

        private ObservableCollection<Dinosaur> _dinoList = new ObservableCollection<Dinosaur>();
        public ObservableCollection<Dinosaur> Dinosaurs
        {
            get { return _dinoList; }
            set { _dinoList = value; RaisePropertyChanged("Dinosaurs"); }
        }
    }

}

Here's how the window is invoked:

// This is a global        
public ViewModel vm = new ViewModel();

// ....

 // Instantiate window
ViewModel vm = new ViewModel();
DinoListDialogBox.DataContext = vm;
DinoListDialogBox.Show();

That should be all the pieces to the puzzle. What am I missing?

Thanks... and I'll name a dinosaur after you.



Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>