2012-08-10 167 views
0

所以我有一個列表框,我正在嘗試做一個「成就頁面」。一切工作好,如果我用一個列表,但是當我切換到列表有顯示,甚至從XAML不是沒事......Windows Phone 7列表框不顯示

public partial class achievementPage : PhoneApplicationPage 
{ 
    public string Description { get; set; } 
    public string Type { get; set; } 
    public achievementPage() 
    { 
     InitializeComponent(); 
     loadListbox(); 
    } 
    public achievementPage(string achievementGet, string d1) 
    { 
    } 
    public void loadListbox() 
    { 
     achievementStoreage.loadData(); 
     List<achievementPage> achievementList = new List<achievementPage>(); 
     achievementList.Add(new achievementPage(achievementStoreage.achievement1, "This is a  test")); 
     achievementList.Add(new achievementPage(achievementStoreage.achievement2, "This is another test")); 
     //List<string> achievementList = new List<string>(); 
     //achievementList.Add("Sup"); 
     achievementListBox.ItemsSource = achievementList; 
    } 
} 

<ListBox Name="achievementListBox" Margin="0,0,0,0" > 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <Button Width="776" Height="120" BorderBrush="Black"> 
       <Button.Content> 
        <StackPanel Orientation="Horizontal" Height="50"> 
         <StackPanel Orientation="Horizontal" Height="40"> 
          <TextBlock Width="150" Foreground="Black" FontSize="22" Text="Description:" Height="40"/> 
         </StackPanel> 
        </StackPanel> 
       </Button.Content> 
      </Button> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

我得到的是一個空白頁。PS不要擔心關於achievementStoreage,它工作正常。(只是我存儲數據的地方)

+0

你在哪裏做與列表框的數據綁定。 ?? – 2012-08-10 23:04:21

+0

他這樣做:'achievementListBox.ItemsSource = achievementList;'在函數'loadListBox'中調用cott .. – quetzalcoatl 2012-08-10 23:12:25

+0

但我在講述如何綁定textblock Text屬性,如下所示Text =「{Binding AchvCount}」 – 2012-08-10 23:14:26

回答

1

坦率地說,看來你是根本不能與UI元素作爲項目DataContexts一起使用的ItemTemplate。我試着:

achievementListBox.ItemsSource = new [] {"a","b"}; 

和這兩個項目分別爲可見光和印刷假的「描述」文本,但沒有以下行我試過的已提出任何東西:

achievementListBox.ItemsSource = new [] { new TextBlock(), new TextBlock()}; 
achievementListBox.ItemsSource = new [] { new Rectangle(), new Rectangle()}; 
achievementListBox.ItemsSource = new [] { new Grid(), new Grid()}; 

嘗試與您的自定義頁面 - 相同。沒有顯示項目。

這很容易讓人誤解。項目顯示爲,但看看上面的行:控件創建,沒有內容設置!

事實證明,如果ListBox中檢測到該項目是的UIElement,那麼它使用的ItemTemplate,但它直接呈現了的UIElement!

achievementListBox.ItemsSource = new[] { new TextBlock() { Text = "bbb" }, new TextBlock() { Text = "eee" } }; 
achievementListBox.ItemsSource = new[] { new Rectangle() { Fill = new SolidColorBrush(Colors.Red), Width = 30, Height = 10 }, new Rectangle() { Fill = new SolidColorBrush(Colors.Green), Width = 30, Height = 10 } }; 

var gridA = new Grid() { Width = 110, Height = 40 }; gridA.Children.Add(new Rectangle() { Fill = new SolidColorBrush(Colors.Red) }); 
var gridB = new Grid() { Width = 110, Height = 40 }; gridB.Children.Add(new Rectangle() { Fill = new SolidColorBrush(Colors.Green) }); 
achievementListBox.ItemsSource = new[] { gridA, gridB }; 

上述所有的三個例子完全忽略ListBox.ItemTemplate,而是,它們顯示直接在兩個項目:兩個文本框,兩個矩形,兩個較大的矩形(在一個網格)。

回到您的案例:這意味着,使用您的原始設置,ListBox將嘗試直接顯示這些項目 - 因爲您的自定義頁面是UIElement。而且的確如此!但是你的網頁是空的。在重載的構造函數中,您省略了通過讀取XAML代碼構造視圖的InitializeComponent();。下面是一個更正的例子,它顯示了「Hello」三次:一次只是因爲它位於頁面上,接下來兩次是因爲ListBox行被設置爲同一頁面。

請原諒我重命名類,我只是開始一個新的項目,而不是粘貼你的代碼。
請注意,我不得不添加到XAML一些其他控件,因爲作爲數據項中的頁面會顯示爲空的,因爲他們沒有任何項目設置

public partial class MainPage : PhoneApplicationPage 
{ 
    public string Description { get; set; } 
    public string Type { get; set; } 

    public MainPage() 
    { 
     InitializeComponent(); 
     loadListbox(); 
    } 

    public MainPage(string achievementGet, string d1) 
    { 
     InitializeComponent(); 
     someText.Text = d1; 
    } 

    public void loadListbox() 
    { 
     achievementListBox.ItemsSource = new[] { new MainPage(null, "ddd"), new MainPage(null, "ccc") }; 
    } 
} 

<StackPanel> 
    <TextBlock> 
     <Run Text="Hello" /> 
     <Run Text=" " /> 
     <Run x:Name="someText" /> 
    </TextBlock> 

    <ListBox Name="achievementListBox" Margin="0,0,0,0"> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <TextBlock Width="150" Foreground="White" 
          FontSize="22" Text="This DataTemplate is IGNORED because the Item is UIElement" 
          Height="40"/> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 
</StackPanel> 

我試圖塑造碼以與你類似的方式,刪除一些與問題無關的行。我希望這可以解釋你現在的一切:)

+0

這就是我正在尋找的,謝謝!令人敬畏的工作btw。 – 2012-08-11 18:11:17

1

哦,我的。爲什麼要創建一個achievementPage s的列表?在您achievementPage你會希望有一個ListBox與類型的項目,如,AchievementItemCompletedAchievementNotCompletedAchievement

目前,什麼也不顯示了,因爲你的代碼可能會拋出StackoverflowException(不是開玩笑在這裏:))。看看:您的achievementPage構造函數調用loadListBox,它創建兩個achievementPages並將它們添加到列表中。但是創建兩個achievementPages會導致它們的構造函數再次被調用兩次,這會調用兩次loadListBox,等等。

- 編輯:ok,no stackoverflow,我剛剛注意到了第二個構造函數。你應該堅持用你知道的大寫字母來命名類:)總之,把Page作爲ListBox的數據項放在Page上是壞主意

你想要得到什麼應該更像:

public partial class AchievementPage : PhoneApplicationPage 
{ 
    public string Description { get; set; } 
    public string Type { get; set; } 

    public AchievementPage() 
    { 
     InitializeComponent(); 
     loadListbox(); 
    } 

    public void loadListbox() 
    { 
     var theList = new List<Achievement>(); 
     theList.Add(new Achievement{ AchvCount=3, AchvName="medals" }); 
     theList.Add(new Achievement{ AchvCount=2, AchvName="badges" }); 
     theList.Add(new Achievement{ AchvCount=6, AchvName="zonks" }); 

     achievementListBox.ItemsSource = achievementList; 
    } 
} 

public class Achievement : DependencyObject 
{ 
    public int AchvCount {get; set;} 
    public string AchvName {get; set;} 
} 

<ListBox Name="achievementListBox" Margin="0,0,0,0"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <Grid> 
       <Grid.ColumnDefinitions> 
        <ColumnDefinition Width="50" /> 
        <ColumnDefinition Width="50" /> 
        <ColumnDefinition Width="50" /> 
       </Grid.ColumnDefinitions> 

       <TextBlock Grid.Column="0" Text="You've got:" /> 
       <TextBlock Grid.Column="0" Text="{Binding AchvCount}" /> 
       <TextBlock Grid.Column="0" Text="{Binding AchvName}" /> 
      </Grid> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 
+0

我已經取得了成就「DependencyObject」,但我已經ommited DependencyProperties的可讀性。如果您想在屬性更改時進行適當的更改通知和UI自動更新 - 您必須添加它們! – quetzalcoatl 2012-08-10 23:14:45

+0

好的建議,但我正在尋找一個答案,爲什麼我的代碼目前不工作,而不是讓它工作的另一種方式。爲我重寫代碼並不能幫助我理解爲什麼當前的代碼無法工作。再次,輸入是非常讚賞。 – 2012-08-11 04:49:13

+0

我明白了,但我手頭沒有WP7設備來測試你的代碼,所以我只是「在內存中分析」。看到第二個構造函數重載,我沒有看到任何錯誤。嘗試使用acquiremntPage/PhoneApplicationPage作爲列表的數據項是很臭的,但它**應該**正常工作,就像你已經在下面用字符串列表嘗試了一樣。我能想象的唯一的事情就是顏色,但是你說bkg是白色的 - 所以不是這樣。我假設XAML是完整的,而不是在ListBox中添加/修改.ItemsSource或.Items道具..我會在星期一嘗試設備。 – quetzalcoatl 2012-08-11 13:56:51