简体   繁体   English

尽管有数据绑定,但删除或添加项目时我的ListBox不会更新

[英]My ListBox doesn't update when deleting or adding items, despite data binding

All my data bindings work throughout the application. 我所有的数据绑定在整个应用程序中都有效。 I've tried every mode for the databinding ItemsSource. 我已经尝试了数据绑定ItemsSource的每种模式。 The List is loaded at the beginning, but doesn't update when I use the context menu I made. 该列表在开始时已加载,但是当我使用自己制作的上下文菜单时不会更新。 I've tested with WriteLine to make sure everything else works, and it does. 我已经对WriteLine进行了测试,以确保其他所有功能都可以正常工作。

XAML XAML

<ListBox Grid.Row="1" SelectionMode="Single" BorderThickness="0" 
    SelectionChanged="ListBox_SelectionChanged"
    SelectedIndex="{Binding Path=Data.SelectedFeedIndex, Mode=TwoWay}" 
    SelectedItem="{Binding Path=Data.SelectedFeed, Mode=TwoWay}" 
    ItemsSource="{Binding Path=Data.Feeds}" 
    ScrollViewer.HorizontalScrollBarVisibility="Disabled">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <Grid>
                <TextBlock Text="{Binding Path=Title}" MouseDown="TextBlock_MouseDown" />
                <TextBox Text="{Binding Path=Title}" 
                    Visibility="Collapsed" KeyDown="TextBox_KeyDown" Padding="1" />
                </Grid>
            </DataTemplate>
        </ListBox.ItemTemplate>
        <ListBox.ContextMenu>
            <ContextMenu>
                <MenuItem Header="New" Command="{Binding Path=Data.NewFeed}" />
                <MenuItem Header="Delete" Command="{Binding Path=Data.DeleteFeed}" />
            </ContextMenu>
        </ListBox.ContextMenu>
        <ListBox.InputBindings>
            <KeyBinding Key="Delete" Command="{Binding Path=Data.DeleteFeed}" />
        </ListBox.InputBindings>
    </ListBox>

The property 物业

private List<Feed> _Feeds = new List<Feed>();
public List<Feed> Feeds
{
    get
    {
        return _Feeds;
    }
    set
    {
        _Feeds = value;
        onPropertyChanged("Feeds");
    }
}

The command 命令

public ICommand DeleteFeed
{
    get
    {
        return new RelayCommand(ExecuteDeleteFeed, CanDeleteFeed);
    }
}

public void ExecuteDeleteFeed(object parameter)
{
    Feeds.RemoveAt(SelectedFeedIndex);
    SelectedFeedIndex = nextIndex;
    onPropertyChanged("Feeds");
}

public bool CanDeleteFeed(object parameter)
{
    return Feeds.Count > 1;
}

I recieve the index through binding with the above ListBox: 我通过与上面的ListBox绑定来接收索引:

private int _SelectedFeedIndex;
public int SelectedFeedIndex
{
    get
    {
        return _SelectedFeedIndex;
    }
    set
    {
        _SelectedFeedIndex = value;
        onPropertyChanged("SelectedFeedIndex");
    }
}

EDIT 编辑

I tried changing to ObservableCollection , but I get the following error. 我尝试更改为ObservableCollection ,但是出现以下错误。

System.Windows.Data Error: 40 : BindingExpression path error: 'Feeds' property not found on 'object' ''Library' (HashCode=60811181)'. BindingExpression:Path=Data.Feeds; DataItem='MainViewModel' (HashCode=34760343); target element is 'ListBox' (Name=''); target property is 'ItemsSource' (type 'IEnumerable')
System.Windows.Data Error: 40 : BindingExpression path error: 'Feeds' property not found on 'object' ''Library' (HashCode=60811181)'. BindingExpression:Path=Data.Feeds; DataItem='MainViewModel' (HashCode=34760343); target element is 'ComboBox' (Name=''); target property is 'ItemsSource' (type 'IEnumerable')

The new property 新物业

ObservableCollection<Feed> Feeds = new ObservableCollection<Feed>();

Oh, okay. 哦好的。 I made it into a property. 我把它变成了财产。 It works now. 现在可以使用了。 Thanks so much. 非常感谢。 I thought you only needed ObservableCollection if you didn't use INotifyProperty manually for all the properties. 我以为,如果您没有为所有属性手动使用INotifyProperty,则只需要ObservableCollection。 It makes sense now. 现在有道理了。

You need to change your List to be an ObservableCollection because the List class does not generate events to notify that its contents have changed. 您需要将List更改为ObservableCollection,因为List类不会生成事件来通知其内容已更改。 As a result the ListBox will show the initial contents of the List but then never update afterwards. 结果,列表框将显示列表的初始内容,但之后再也不会更新。 ObservableCollection does generate change events and will do what you need. ObservableCollection确实会生成更改事件,并将执行您需要的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM