2011-04-04 53 views
0

如果列表框中没有项目,尝试可能会显示“无数据”。由于我在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 
+0

而你在哪里设置页面的ItemsSource或DataContext的? – 2011-04-04 14:15:09

+0

在 – argh 2011-04-04 14:37:19

+0

后添加了代码,什么是'ClientModel'? – 2011-04-04 15:12:51

回答

0

创建的ItemsSource为DependencyProperty

例子:

public IEnumerable ItemsSource 
{ 
    get { return (IEnumerable)base.GetValue(ItemsSourceProperty); } 
    set { base.SetValue(ItemsSourceProperty, value); } 
} 

public static DependencyProperty ItemsSourceProperty = 
      DependencyProperty.Register(
      "ItemsSource", 
      typeof(IEnumerable), 
      typeof(EmptyListBox), 
      new PropertyMetadata(null)); 
+0

这不起作用......给名称添加'新'也没有奏效......注意到我是继承ListBox – argh 2011-04-04 14:33:38

0

尝试实现INotifyPropertyChanged界面和使用的ItemsSource一个ObservableCollection。在属性的Setter中,只需调用OnPropertyChanged方法即可。

也许这会有所帮助。

+0

客户端模型正在继承INotifyPropertyChanged并且数据正确填充。问题是我不知道为什么我的东西没有被调用。 – argh 2011-04-04 14:58:51

+0

噢,我明白了。 hm – cordellcp3 2011-04-04 15:09:40

0

尝试增加模式=双向到的ItemsSource绑定:

<my:EmptyListBox ItemsSource="{Binding Path=MyData, Mode=TwoWay}" Name="myListBox"> 
    <my:EmptyListBox.ItemTemplate> 
     <DataTemplate> 
      <TextBlock Text="{Binding Path=name}" /> 
     </DataTemplate> 
    </my:EmptyListBox.ItemTemplate> 
</my:EmptyListBox> 
+0

也没有工作 - 绑定工程,但它看起来像这个类忽略了我已经在我的派生类中重载了ItemsSource的事实 – argh 2011-04-05 19:35:21

1

您的新的ItemSource应该是一个DependencyProperty。 任何与Bindings一起工作的东西都必须是DependencyProperty。 只需将其设置为DependencyProperty即可。

+0

纠正我,如果我错了,但因为我从ListBox派生,然后它应该已经是一个DependencyProperty? – argh 2011-04-07 07:48:23

+0

当您声明dependencyProperty时,您还指定ownerType。 在你的情况下,ownerType是EmptyListBox。(与ItemsSource的ItemsControl相比) 你需要声明你自己的DependencyProperty,但似乎你尝试了,但它没有帮助,这很奇怪。 无论如何,也许我们在这里还有另一个问题,但是ItemsSource应该是DependencyProperty的事实是肯定的。 – Sonosar 2011-04-07 10:07:49

1

我认为解决的办法我会去是这样的:

  1. 定义一个新的视觉状态组ItemsStates和两个可视状态:NoItemsHasItems
    1. 在自定义列表框的ControlTemplate中,为“无数据”状态添加可视化树。
    2. NoItems状态,请将您的“无数据”元素的VisibilityVisible和设置默认ItemsPresenterVisibilityCollapsed
    3. HasItems状态下,交换Visibility这些元素。
    4. OnApplyTemplate超控开关的状态默认为:VisualStateManager.GoToState(this, "Empty", true);
    5. 在一个OnItemsChanged倍率,检查物品源是否是空的,并使用相应VisualStateManager这些状态之间切换。

这应该工作:)

相关问题