属性更改时WPF UI未更新

Fri*_*ale 7 wpf user-interface

我不确定我在这里做错了什么......

我有一个自定义HashTable,它有一个允许某人从HashTable中删除"partNumber"(值)的方法.

删除方法如下:

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));
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我将HashTable的基础值绑定到UI; 但是,在删除值后,UI不会更新.我确保拼写等没有问题......

这是我的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>
Run Code Online (Sandbox Code Playgroud)

这是我调用的代码,用于在单击按钮时删除HashTable中的项目:

  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;

        }
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

谢谢,

-Frinny

ben*_*rce 5

首先在"输出"窗口中查找绑定错误,这通常会指向正确的方向.

如果您绑定到自定义集合,则可能需要实现INotifyCollectionChanged.或者考虑将数据源更改为ObservableCollection,或者在您的情况下,您可能需要ObservableDictionary.

你还提到了拼写错误,有几种方法可以确保这不是问题,请查看MVVM Foundation的基础ObservableObject

您的代码缺少一些细节,例如您对StaticResource TheHashTable的声明.

编辑:针对List类提升PropertyChanged将不会通知该List中的更改,如果您需要UI来查看列表中的更改,请将列表类型更改为ObservableCollection或创建新属性:

public ObservableCollection Hash
{
  get
  {
    return new ObservableCollection(this.HashList);
  }
}
Run Code Online (Sandbox Code Playgroud)

并绑定到Hash属性.