【问题标题】:Trouble with DataBinding to a ListBox in WP7 which is not displaying the bound contentDataBinding 到 WP7 中未显示绑定内容的 ListBox 的问题
【发布时间】:2012-02-17 21:16:54
【问题描述】:

我在 ListBox 上有一个 DataBinding,绑定到 ObservableCollection。在运行时调试显示 ObservableCollection 确实 中有项目,并且它们不为空。我的代码看起来都很好,但是由于某种原因,我的 ListBox 中没有显示任何内容。它以前肯定是有效的,但它不再是 - 我不知道为什么。我检查了以前版本的代码,发现没有任何差异会对此产生任何影响 - 诸如 Width="Auto" 之类的小问题。

我的代码基于此处找到的示例:

http://msdn.microsoft.com/en-us/library/hh202876.aspx

所以,我的代码:

XAML:

<phone:PhoneApplicationPage 
x:Class="MyNamespace.MyItemsListPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
FontFamily="{StaticResource PhoneFontFamilyNormal}"
FontSize="{StaticResource PhoneFontSizeNormal}"
Foreground="{StaticResource PhoneForegroundBrush}"
SupportedOrientations="Portrait" Orientation="Portrait"
mc:Ignorable="d" d:DesignHeight="768" d:DesignWidth="480"
shell:SystemTray.IsVisible="True">

<Grid x:Name="LayoutRoot" Background="Transparent">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <TextBlock Grid.Row="0" x:Name="PageTitle" Text="MyPageTitle" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>

    <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
        <!-- Bind the list box to the observable collection. -->
        <ListBox x:Name="myItemsListBox" ItemsSource="{Binding MyItemsList}" Margin="12, 0, 12, 0" Width="440">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid HorizontalAlignment="Stretch" Width="440">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="80" />
                            <ColumnDefinition Width="*" />
                        </Grid.ColumnDefinitions>
                        <TextBlock
                            Text="{Binding MyItemNumber}"
                            FontSize="{StaticResource PhoneFontSizeLarge}"
                            Grid.Column="0"
                            VerticalAlignment="Center" 
                            Margin="0,10"
                            Tap="TextBlock_Tap"/>
                        <TextBlock
                            Text="{Binding MyItemName}"
                            FontSize="{StaticResource PhoneFontSizeLarge}"
                            Grid.Column="1"
                            VerticalAlignment="Center"
                            Margin="0,10" 
                            Tap="TextBlock_Tap" />
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </Grid>
</Grid>
</phone:PhoneApplicationPage>

C#:

namespace MyNamespace
{
public partial class MyItemsListPage : PhoneApplicationPage, INotifyPropertyChanged
{
    private static ObservableCollection<MyItem> _myItemsList;
    private ObservableCollection<MyItem> MyItemsList
    {
        get
        {
            return _myItemsList;
        }
        set
        {
            if (_myItemsList!= value)
            {
                _myItemsList= value;
                NotifyPropertyChanged("MyItemsList");
            }
        }

    }

    public MyItemsListPage ()
    {
        InitializeComponent();
        this.DataContext = this;
    }

    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        HelperClass helper = new HelperClass();
        MyItemsList = helper.GetItems(this.NavigationContext.QueryString["query"]);
        base.OnNavigatedTo(e); // Breakpoint here shows "MyItemsList" has MyItem objects in it.
    }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    // Used to notify Silverlight that a property has changed.
    private void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    #endregion
}
}

Helper 类是设备上我的只读本地数据库的连接器。它返回一个ObservableCollection&lt;MyItem&gt;

public ObservableCollection<MyItem> GetItems(string itemName)
    {
        // Input validation etc.
        // Selecting all items for testing
        var itemsInDB =
            from MyItem item in db.Items
            select item;
        return new ObservableCollection<MyItem>(itemsInDB);
    }

最后是 MyItem 类:

[Table]
public class MyItem: INotifyPropertyChanged, INotifyPropertyChanging
{
    private int _myItemId;

    [Column(IsPrimaryKey = true, IsDbGenerated = true, DbType = "INT NOT NULL Identity", CanBeNull = false, AutoSync = AutoSync.OnInsert)]
    public int MyItemId
    {
        ...
    }

    private string _myItemName;

    [Column(CanBeNull = false)]
    public string MyItemName
    {
        get
        {
            return _myItemName;
        }
        set
        {
            if (_myItemName!= value)
            {
                NotifyPropertyChanging("MyItemName");
                _myItemName= value;
                NotifyPropertyChanged("MyItemName");
            }
        }
    }

    private int _myItemNumber;
    [Column]
    public int MyItemNumber
    {
        get
        {
            return _myItemNumber;
        }
        set
        {
            if (_myItemNumber!= value)
            {
                NotifyPropertyChanging("MyItemNumber");
                _myItemNumber= value;
                NotifyPropertyChanged("MyItemNumber");
            }
        }
    }
    // Other properties, NotifyPropertyChanged method, etc...
}

这相当令人沮丧,因为我在应用程序其他地方的 DataBinding 运行良好,所以我不知道为什么我不能让它工作。

【问题讨论】:

    标签: silverlight windows-phone-7 data-binding listbox


    【解决方案1】:

    问题是我的 ObservableCollection 是 private。将其更改为具有 public 访问修饰符允许我的 ListBox 显示内容:

    public ObservableCollection<MyItem> MyItemsList
    

    【讨论】:

      【解决方案2】:

      只是您绑定到名称不正确的属性:

      Text="{Binding ItemName}" 应该是Text="{Binding MyItemName}"

      请注意您遗漏了“我的”

      【讨论】:

      • 很遗憾没有这么简单,我只是在编辑这个问题的代码时拼错了名字。
      猜你喜欢
      • 2012-04-28
      • 2011-06-05
      • 2011-08-14
      • 2017-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多