繁体   English   中英

WPF ListBox属性绑定未更新

[英]WPF ListBox property binding not updating

我有以下设置:

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只是控制可见性是“ Collapsed”( False )还是“ Hidden”( 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列表的IItem从数据库填充到列表中,并且如果HasInformation为true,则会适当显示信息图标。 这一切都正常工作。

但是,如果我手动设置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属性的吸气剂, 但是视图永远不会更新,图标也永远不会显示。 实际上,我确保我更新的项目的HasInformation属性确实保持为true 我已经附加了PropertyChanged事件,以确保它触发了"MyItems"的属性更改(即,这也触发了吸气剂),并且甚至确保它为HasInformation调用了具有正确值的值转换器。属性(是!),那么我想念的是什么? 图片显示/隐藏或可见度值转换是否有些奇怪,我无法正确处理?

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

暂无
暂无

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

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