2012-02-17 6 views
0

ObservableCollectionにバインドされたListBoxにDataBindingがあります。実行時のデバッグでは、ObservableCollection にはに項目があり、nullではありません。私のコードはすべてうまく見えますが、何らかの理由で何も私のListBoxに表示されていません。それは間違いなく前に働いていましたが、もはやそれはありません。なぜそれが分からないのですか?私は、コードの以前のバージョンを調べて、この上の任意の効果を持っているでしょう差異は認められなかっました - 幅のようなマイナーなものは=「オート」などバインドされたコンテンツを表示していないWP7のListBoxへのデータバインドに関する問題

を、私はここで見つける例のオフに私のコードをベース:

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クラスは、デバイスの読み取り専用ローカルデータベースへのコネクタです。

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

これは、アプリケーションの他の場所で私のデータバインディングとしてではなくイライラ完璧に働いているので、私はさっぱりだが、なぜ私に」それはObservableCollection<MyItem>返しますこれを働かせてください。

答えて

1

問題は、私のObservableCollectionがprivateということでした。それは私のリストボックスは、コンテンツを表示することができpublicアクセス修飾子を持つように変更する:この質問のためのコードを編集するとき

public ObservableCollection<MyItem> MyItemsList 
0

あなたは間違って名前付きプロパティに結合しているだけであること:

Text="{Binding ItemName}"はあなたが取り残さText="{Binding MyItemName}"

お知らせする必要があります「マイ」

+0

残念ながら、それはこの単純ではない、私は間違った名前を綴りました。 –

関連する問題