如果列表框中没有项目,尝试可能会显示“无数据”。由于我在wp7上并使用Silverlight,因此我无法使用DataTriggers,所以我创建了一个控件,让它在整个应用程序中保持一致。但是,如果你为set方法设置了断点 - 它根本没有被调用!EmptyListBox控件不会收到通知ItemsSouce已更改(silverlight/wp7)
控制类
public class EmptyListBox : ListBox
{
public new IEnumerable ItemsSource
{
get
{
return base.ItemsSource;
}
set
{
// never here
base.ItemsSource = value;
ItemsSourceChanged();
}
}
protected virtual void ItemsSourceChanged()
{
bool noItems = Items.Count == 0;
if (noItems)
{
if (Parent is System.Windows.Controls.Panel)
{
var p = Parent as Panel;
TextBlock noData = new TextBlock();
noData.Text = "No data";
noData.HorizontalAlignment = HorizontalAlignment;
noData.Width = Width;
noData.Height = Height;
noData.Margin = Margin;
p.Children.Add(noData);
Visibility = System.Windows.Visibility.Collapsed;
}
}
}
}
这是XAML
<my:EmptyListBox ItemsSource="{Binding Path=MyData}" Name="myListBox">
<my:EmptyListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=name}" />
</DataTemplate>
</my:EmptyListBox.ItemTemplate>
</my:EmptyListBox>
代码隐藏:
ClientModel ClientInfo { get; set; }
public ClientView()
{
ClientInfo = new ClientModel();
ClientInfo.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(DataReady);
DataContext = ClientInfo
}
ClientModel类:
public class ClientModel : INotifyPropertyChanged
{
MyData _myData;
public MyData MyData
{
get
{
return _myData;
}
set
{
_myData = value;
NotifyPropertyChanged("MyData");
}
}
public void GetClient(int id)
{
// fetch the network for data
}
}
LINK TO SOLUTION .ZIP,显示问题
http://rapidshare.com/files/455900509/WindowsPhoneDataBoundApplication1.zip
而你在哪里设置页面的ItemsSource或DataContext的? – 2011-04-04 14:15:09
在 – argh 2011-04-04 14:37:19
后添加了代码,什么是'ClientModel'? – 2011-04-04 15:12:51