0

如果列表框中没有项目,尝试可能会显示“无数据”。由于我在 wp7 上并使用 silverlight,因此我无法使用 DataTriggers,因此我创建了一个控件以使其在整个应用程序中的行为保持一致。但是我如果你为 set 方法设置断点 - 它根本不会被调用!

控制类

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;
            }
        }
    }
}

这是 xml

<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
    }

客户端模型类:

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
    }
}   

显示问题的解决方案 .ZIP 的链接

http://rapidshare.com/files/455900509/WindowsPhoneDataBoundApplication1.zip
4

5 回答 5

1

I think the solution I'd go for is something like this:

  1. Define a new visual state group ItemsStates and two visual states: NoItems and HasItems.
    1. In the ControlTemplate for your custom listbox, add the visual tree for your "no data" state.
    2. In the NoItems state, set the Visibility of your "no data" elements to Visible and set the Visibility of the default ItemsPresenter to Collapsed.
    3. In the HasItems state, swap the Visibility of these elements.
    4. In an OnApplyTemplate override switch to the Empty state by default: VisualStateManager.GoToState(this, "Empty", true);
    5. In an OnItemsChanged override, check whether the items source is empty and use VisualStateManager to switch between these states accordingly.

That should work :)

于 2011-04-10T13:10:15.117 回答
1

您的新 ItemSource 应该是 DependencyProperty。任何使用绑定的东西都必须是 DependencyProperty。只需将其设为 DependencyProperty。

于 2011-04-06T19:54:16.877 回答
0

尝试实现INotifyPropertyChanged接口并为 ItemsSource 使用 ObservableCollection。在您的 Property 的 Setter 中,只需调用 OnPropertyChanged 方法。

也许这会有所帮助。

于 2011-04-04T14:50:15.147 回答
0

尝试将 Mode=TwoWay 添加到 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>
于 2011-04-04T20:11:30.470 回答
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));
于 2011-04-04T14:13:30.183 回答