WPF ListBox属性绑定不更新

时间:2014-03-20 23:09:55

标签: c# wpf mvvm binding listbox

我有以下设置:

XAML:

<ListBox x:Name="MyList" ItemsSource="{Binding MyItems}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <Image Height="20" Width="20" Visibility="{Binding HasInformation, Converter={StaticResource VC}, ConverterParameter=True}" Source="/path/to/information.png" />
                <TextBlock Text="{Binding Name}" VerticalAlignment="Center" Padding="5,0" />
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

注意:传入的ConverterParameter只是控制可见性是“折叠”(False)还是“隐藏”(True),所以在这种情况下,我想要可见性为Hidden

ViewModel代码段

private ObservableCollection<IItem> _MyItems;
public ObservableCollection<IItem> MyItems
{
    get
    {
        return _MyItems;
    }
    set
    {
        NotifyPropertyChanged(ref _MyItems, value, "MyItems");
    }
}

private IItem _SelectedItem;
public IItem SelectedItem
{
    get
    {
        return _SelectedItem;
    }
    set
    {
        NotifyPropertyChanged(ref _SelectedItem, value, "SelectedItem");
    }
}

的iItem:

public interface IItem
{
    string Name { get; }
    bool HasInformation { get; set; }
}

我将数据库中IItem列表的实现填充到列表中,如果HasInformation为真,则信息图标会正确显示。一切正常。

但是,如果我手动设置HasInformation,则视图不会更新。我试过了:

在ViewModel中:

OnPropertyChanged("MyItems");
MyItems[MyItems.IndexOf(SelectedItem)].HasInformation = true;
// Note that "SelectedItem" is persisted correctly, and always
// points to the selected item that we want to update.

在背后的代码中:

MyList.GetBindingExpression(ItemsControl.ItemsSourceProperty).UpdateTarget();

所有这些都会触发MyItems属性的getter,但视图永远不会更新,图标也不会显示。我已确保项目的HasInformation属性事实上,我更新的确是true。我已经附加了PropertyChanged事件,以确保它为"MyItems"触发属性更改(它也会触发getter),我甚至确保它正在调用值转换器HasInformation属性的正确值(它是!),所以我错过了什么?图像显示/隐藏或可见性值转换是否有些奇怪我无法正确处理?

1 个答案:

答案 0 :(得分:4)

ObservableCollection仅通知集合更改而不是每个项目中的更改。为了实现您的目标,其中一个选项是将IItem从接口更改为实现INotifyPropertyChanged接口的类(或在IItem具体类型中实现它),并将其与ViewModel的PropertyChanged委托挂钩(记得取消订阅)它)。请参阅下面的一些代码。

<强>视图模型

public class MyViewModel: INotifyPropertyChanged
{
    private ObservableCollection<Item> _MyItems;
    public ObservableCollection<Item> MyItems
    {
        get
        {
            return _MyItems;
        }
        set
        {
            if (_MyItems != null)
            {
                foreach (var item in _MyItems)
                {
                    item.PropertyChanged -= PropertyChanged;
                }
            }

            if (value != null)
            {
                foreach (var item in value)
                {
                    item.PropertyChanged += PropertyChanged;
                }
            }

            OnPropertyChanged();
        }
    }

    private Item _SelectedItem;
    public Item SelectedItem
    {
        get
        {
            return _SelectedItem;
        }
        set
        {
            _SelectedItem = value;
            OnPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

<强>物品

public class Item : INotifyPropertyChanged
{
    private string _name;
    private bool _hasInformation;

    public string Name
    {
        get { return _name; }
        set
        {
            _name = value;
            OnPropertyChanged();
        }
    }

    public bool HasInformation
    {
        get { return _hasInformation; }
        set
        {
            _hasInformation = value;
            OnPropertyChanged();
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        var handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
}