【问题标题】:My ListBox doesn't update when deleting or adding items, despite data binding尽管数据绑定,但删除或添加项目时,我的 ListBox 不会更新
【发布时间】:2015-04-12 12:26:55
【问题描述】:

我的所有数据绑定在整个应用程序中都有效。我已经尝试了数据绑定 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。现在说得通了。

【问题讨论】:

    标签: wpf data-binding listbox


    【解决方案1】:

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

    【讨论】:

    • 即使对类中的所有属性都使用INotifyProperty也不起作用?我现在正在尝试,但出现错误。我会把它添加到帖子中。
    • 啊,您需要在集合类本身上添加 INotifyCollectionChanged,以便 ListBox 可以看到何时从集合中添加和删除项目。 INotifyPropertyChanged 将用于 Feed 类以通知属性值的更改。
    猜你喜欢
    • 2014-08-25
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-15
    • 2018-12-14
    • 2021-09-30
    • 1970-01-01
    相关资源
    最近更新 更多