簡體   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