您也可以在C#視圖模型中執行此邏輯。無需更改您的xaml代碼。
public sealed partial class MainPage : Page, INotifyPropertyChanged {
private ObservableCollection<string> recentPatients = new ObservableCollection<string>();
private IList<string> emptyList = new string[] { "This list is empty" };
public MainPage() {
this.InitializeComponent();
this.DataContext = this;
this.recentPatients.CollectionChanged += OnCollectionChanged;
}
public IList<string> RecentPatients {
get { return recentPatients.Any() ? recentPatients : emptyList; }
}
private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) {
if (this.recentPatients.Count <= 1) {
// It could be a change between empty to non-empty.
this.OnPropertyChanged("RecentPatients");
}
}
// implement the INotifyPropertyChanged pattern here.
那麼它會工作與空列表,而不是空? – MBen 2012-07-13 11:54:05
好點,檢查一下:http://stackoverflow.com/questions/699881/wpf-listbox-empty-datatemplate – 2012-07-13 11:57:11