0

だから私は「達成ページ」のために作ろうとしているリストボックスを持っています。リストを使用するとすべてが正常に機能しますが、リストに切り替えると、xaml からでも何も表示されません...

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について心配しないでください、それは適切に機能しています.(ちょうど私が保存されたデータを取得する場所)

4

2 に答える 2

1

なんてこった。achievementPages のリストを作成するのはなぜですか? あなたの achievementPage には、、、 などListBoxのタイプのアイテムが必要です。AchievementItemCompletedAchievementNotCompletedAchievement

現在、コードはおそらく StackoverflowException をスローするため、何も表示されません (冗談ではありません:))。見てください: あなたの achievementPage コンストラクターは、2 つの achievementPages を作成し、それらをリストに追加する loadListBox を呼び出します。しかし、2 つの achievementPages を作成すると、それらのコンストラクターが再び 2 回呼び出され、loadListBox が 2 回呼び出されます。

-- 編集: わかりました、stackoverflow はありません。2 番目のコンストラクターに気付きました。あなたが知っている大文字でクラスに名前を付けることに固執する必要があります:)とにかく、Pagea のデータ項目として aListBoxを置くの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>
于 2012-08-10T23:03:43.360 に答える
1

率直に言って、アイテムの DataContext として UIElements と一緒に ItemTemplates を使用することは許可されていないようです。私は試してみました:

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

上記の 3 つの例はすべて ListBox.ItemTemplate を完全に無視し、代わりに 2 つの項目を直接表示します: 2 つのテキスト ボックス、2 つの四角形、2 つの大きな四角形 (グリッド内)。

ケースに戻る: 元の設定では、カスタム ページが UIElement であるため、ListBox も項目を直接表示しようとすることを意味します。そして実際にそれを行いました!しかし、あなたのページは...空でした。InitializeComponent();オーバーロードされたコンストラクターでは、XAML コードを読み取ってビューを構築する を省略しました。これは、"Hello" を 3 回表示する修正された例です。1 回はページ上にあるという理由だけで、次の 2 回は 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>

私はあなたと同様の方法でコードを形作ろうとしましたが、問題に関係のないいくつかの行を削除しました。これですべてが説明されることを願っています:)

于 2012-08-11T14:23:56.333 に答える