Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF UI not updated upon property change

I'm not sure what I'm doing wrong here...

I have a custom HashTable that has a method that allows someone to delete a "partNumber" (a value) from the HashTable.

The delete method is as follows:

class COSC202HashTable : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;

    //....
    private List<int> underlyingList;
    //... 
    public List<int> HashList { get { return underlyingList; } }

    public void Delete(int partNumber)
    {
            string theAlgoritnm = Algorithm;
            if (String.Compare(theAlgoritnm, "Modulo Division") == 0 && String.Compare(Probe, "Linear Collision Resolution") == 0)
            {
                LinearModularDivision(partNumber, false);
            }
            if (String.Compare(theAlgoritnm, "Modulo Division") == 0 && String.Compare(Probe, "Key Offset Collision Resolution") == 0)
            {
                KeyOffsetModularDivision(partNumber, false);
            }
            if (String.Compare(theAlgoritnm, "Pseudorandom") == 0)
            {
                Pseudorandom(partNumber, false);
            }
            if (String.Compare(theAlgoritnm, "Rotation") == 0)
            {
                Rotation(partNumber, false);
            }

            NotifyPropertyChanged("HashList");
    }
   //.......
    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }
}

I am binding the HashTable's underlying values to the UI; however the UI is not being updated after a value is deleted. I made sure that there are no problems with spelling etc...

This is the markup I have for my WPF UI:

<Window.Resources>
    <COSC202:COSC202HashTable x:Name="TheHashTable"  x:Key="TheHashTable" PropertyChanged="TheHashTable_PropertyChanged"></COSC202:COSC202HashTable>
</Window.Resources>
<ListView x:Name="HashResults" Height="32" Width="1200" Margin="10"  HorizontalAlignment="Right"
                      DataContext="{Binding Source={StaticResource TheHashTable}}" ItemsSource="{Binding Path=HashList}" HorizontalContentAlignment="Left">
    <ListView.Background>
        <LinearGradientBrush StartPoint="0,0" EndPoint="0,2">
            <GradientStop Color="#FF000000" Offset="0"></GradientStop>
            <GradientStop Color="DarkBlue" Offset="1"></GradientStop>
        </LinearGradientBrush>
    </ListView.Background>
    <ListView.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal"></StackPanel>
        </ItemsPanelTemplate>
    </ListView.ItemsPanel>

    <ListView.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal" >
                <TextBlock Text="{Binding Path=.}" FontSize="11" Foreground="Azure" VerticalAlignment="Top" ></TextBlock>
                <Label Content="|" VerticalAlignment="Top" FontSize="5"></Label>
            </StackPanel>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

This is the code that I am calling to delete the item in the HashTable upon button click:

  private void DeleteItem_Click(object sender, RoutedEventArgs e)
        {
            Object item = HashResults.SelectedItem;
            COSC202HashTable theHashTable = (COSC202HashTable)this.Resources["TheHashTable"];
            if (theHashTable != null && item != null)
            {
                theHashTable.Delete((int)item);
            }
            HashResults.SelectedIndex = -1;

        }

What am I doing wrong?

Thanks,

-Frinny

like image 942
Frinavale Avatar asked Oct 24 '10 04:10

Frinavale


1 Answers

The first place to look for binding errors in the Output window, this quite often will point you in the right direction.

If you are binding to a custom collection then you may need to implement INotifyCollectionChanged. Or consider changing your data source to ObservableCollection, or in your case you may need ObservableDictionary.

Also you mentioned spelling errors, there are a couple of ways to ensure that this is not an issue, check out MVVM Foundation's base ObservableObject

Your code is missing a few details, such as your declaration for the StaticResource TheHashTable.

Edit: Raising PropertyChanged against a List class will not notify of changes within that List, if you need the UI to see changes within the list change the list type to ObservableCollection or create a new property:

public ObservableCollection Hash
{
  get
  {
    return new ObservableCollection(this.HashList);
  }
}

and bind to the Hash property.

like image 98
benPearce Avatar answered Nov 06 '22 01:11

benPearce