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

Problem with binding (need change in data to automatically update display)

$
0
0

I've got a very nice listview display (just a portion shown below):

Created from this XAML:

<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>

That uses this class:

namespace DinosaurIsland
{
    public class ViewModel : INotifyPropertyChanged
    {
        public ViewModel()
        {
           this.Dinosaurs = new List<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));
            }

        }



        public List<Dinosaur> Dinosaurs
        {
            get;
            set;
        }
    }

}

And is called like so (note: vm is a global) snippet below:

 public ViewModel vm = new ViewModel();

//
          vm.Dinosaurs = Dinosaurs; 
            DinoListDialogBox.DataContext = vm;
            DinoListDialogBox.Show();


When a change is made to the global list:

 public static List<Dinosaur> Dinosaurs = new List<Dinosaur>();

Anywhere in the program I want the dialog box (shown at the top) to reflect the updated data.

I'm missing something, probably having to do with the get in ViewModel class but everything I've tried is throwing errors. Help would be greatly appreciated! Thanks!


Viewing all articles
Browse latest Browse all 18858

Trending Articles



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