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

时间:2015-02-11 22:52:42

标签: wpf data-binding listbox

我的所有数据绑定都适用于整个应用程序。我已经尝试了数据绑定ItemsSource的每个模式。列表在开头加载,但在我使用我创建的上下文菜单时不会更新。我已经使用WriteLine进行了测试,以确保其他所有功能正常运行。

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>

属性

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

命令

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

我通过绑定上面的ListBox来接收索引:

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

修改

我尝试更改为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')

新属性

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

哦,好的。我把它变成了一个财产。它现在有效。非常感谢。我认为如果你没有为所有属性手动使用INotifyProperty,你只需要ObservableCollection。现在这很有意义。

1 个答案:

答案 0 :(得分:1)

您需要将List更改为ObservableCollection,因为List类不会生成事件来通知其内容已更改。因此,ListBox将显示List的初始内容,但之后永远不会更新。 ObservableCollection确实会生成更改事件,并且会执行您需要的操作。