我從Stackoverflow answer複製了ObservableStack。Observable Stack <T>無法正常工作
public class ObservableStack<T> : Stack<T>, INotifyCollectionChanged, INotifyPropertyChanged
{
public ObservableStack()
{
}
public ObservableStack(IEnumerable<T> collection)
{
foreach (var item in collection)
base.Push(item);
}
public ObservableStack(List<T> list)
{
foreach (var item in list)
base.Push(item);
}
public new virtual void Clear()
{
base.Clear();
this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
public new virtual T Pop()
{
var item = base.Pop();
this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, item));
return item;
}
public new virtual void Push(T item)
{
base.Push(item);
this.OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, item));
}
public virtual event NotifyCollectionChangedEventHandler CollectionChanged;
protected virtual void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
{
this.RaiseCollectionChanged(e);
}
protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
{
this.RaisePropertyChanged(e);
}
protected virtual event PropertyChangedEventHandler PropertyChanged;
private void RaiseCollectionChanged(NotifyCollectionChangedEventArgs e)
{
if (this.CollectionChanged != null)
this.CollectionChanged(this, e);
}
private void RaisePropertyChanged(PropertyChangedEventArgs e)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, e);
}
event PropertyChangedEventHandler INotifyPropertyChanged.PropertyChanged
{
add { this.PropertyChanged += value; }
remove { this.PropertyChanged -= value; }
}
}
,現在我嘗試使用下面的轉換器類綁定按鈕來Stack.Count能見度:
public class StackToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
var collection = (Stack<int>)value;
return collection.Count > 0 ? Visibility.Visible : Visibility.Collapsed;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
我在代碼中定義的堆後面爲公共財產
public ObservableStack<int> myStack;
public MainPage()
{
myStack = new ObservableStack<int>();
this.InitializeComponent();
myButton.DataContext = myStack;
}
和XAML如下:
<Page.Resources>
<Converters:StackToVisibilityConverter x:Key="StackToVisibilityConverter"/>
</Page.Resources>
<Button x:Name="myButton" Content="Test Button" Visibility="{Binding Converter={StaticResource StackToVisibilityConverter}}"/>
現在,當我使用另一個按鈕單擊事件來推動一個int時,該按鈕仍然是不可見的,我做錯了什麼?
當添加或刪除項目(或其他任何事情發生時)時,我無法找到爲「Count」引發'PropertyChanged'的代碼。我想知道Stack上的任何東西是否會打電話給你的OnPropertyChanged;當你在那裏設置斷點時,你有沒有打過它? 「新」東西也值得懷疑。我會清理這個混亂,並寫我自己的可觀察堆棧,可能派生自ObservableCollection。無論是那個,還是我只爲ObservableCollection編寫Push和Pop擴展方法,然後出去喝啤酒。 –