如何在列表框控件中水平显示项目?

时间:2011-02-12 13:53:12

标签: silverlight windows-phone-7 listbox stackpanel

我正在开发window phone 7应用程序。我是窗口手机7应用程序的新手。我的应用程序中有以下列表框控件

<ListBox Margin="0,355,70,205" Name="WeekSummaryListBox" DataContext="{Binding}">
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock TextWrapping="Wrap" Width="150" Text="{Binding Amount}" Foreground="Orange"></TextBlock>
                <TextBlock TextWrapping="Wrap" Width="150" Text="{Binding Currency}" Foreground="Orange"></TextBlock>                            
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>

    <ListBox.Template>
        <ControlTemplate>
            <ScrollViewer HorizontalScrollBarVisibility="Visible">
                <ItemsPresenter />
            </ScrollViewer>
        </ControlTemplate>
    </ListBox.Template>
</ListBox>

在上面的列表框中,控件项目是垂直显示的。我想在列表框控件中显示项目。所以我使用以下代码。

<ListBox.ItemsPanel>
    <ItemsPanelTemplate>
        <StackPanel Orientation="Horizontal"/>
    </ItemsPanelTemplate>
</ListBox.ItemsPanel>

但是当我将两个文本块放在stackpanel中时,它会出错。为此,我使用以下代码

<StackPanel Orientation="Horizontal">
    <TextBlock TextWrapping="Wrap" Width="150" Text="{Binding Amount}" Foreground="Orange"></TextBlock>
    <TextBlock TextWrapping="Wrap" Width="150" Text="{Binding Currency}" Foreground="Orange"></TextBlock>                            
</StackPanel>

我正在尝试以下代码。在以下代码中我收到错误

<ListBox Margin="0,355,70,205" Name="WeekSummaryListBox" DataContext="{Binding}">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal">
                <TextBlock TextWrapping="Wrap" Width="150" Text="{Binding Amount}" Foreground="Orange"></TextBlock>
                <TextBlock TextWrapping="Wrap" Width="150" Text="{Binding Currency}" Foreground="Orange"></TextBlock>
            </StackPanel>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <ListBox.Template>
        <ControlTemplate>
            <ScrollViewer HorizontalScrollBarVisibility="Visible">
                <ItemsPresenter />
            </ScrollViewer>
        </ControlTemplate>
    </ListBox.Template>
</ListBox>

我收到错误&#34;无法显式修改用作ItemsControl的ItemsPanel的Panel的子集合.ItemsControl生成Panel&#34;

的子元素

我使用以下函数在ListBox中显示数据:

public void WeekSumValue(int TransactionType_ID)
{
    UserInterfaceManager UserInterfaceManagerObj = new UserInterfaceManager();
    List<UserInterfaceManager.TotalSummary> WeekSummary = new List<UserInterfaceManager.TotalSummary>();           
    ObservableCollection<AmountCurrency> WeekIncomeSumCollection = new ObservableCollection<AmountCurrency>();

    WeekSummary = UserInterfaceManagerObj.LoadWeekSum(SelectedButtonName, TransactionType_ID, selectedDate);

    foreach (UserInterfaceManager.TotalSummary vWeekSummary in WeekSummary)
    {
        WeekIncomeSumCollection.Add(new AmountCurrency(vWeekSummary.Amount, vWeekSummary.Currency));
    }

    if (WeekIncomeSumCollection.Count != 0 && SummaryCombobox.SelectedIndex == 0)
    {
        WeekSummaryListBox.ItemsSource = WeekIncomeSumCollection;
    }
    else if (WeekIncomeSumCollection.Count != 0 && SummaryCombobox.SelectedIndex == 2)
    {
        MonthSummaryListBox.ItemsSource = WeekIncomeSumCollection;
    }
    else
    {
        ObservableCollection<TextBlock> NoRecordsCollection = new ObservableCollection<TextBlock>();
        TextBlock NoRecordsTextBlock = new TextBlock();
        NoRecordsTextBlock.Text = "No record found";
        NoRecordsTextBlock.FontSize = 25;
        NoRecordsTextBlock.Foreground = new SolidColorBrush(Colors.Gray);
        NoRecordsCollection.Add(NoRecordsTextBlock);

        if (SummaryCombobox.SelectedIndex == 0)
            WeekSummaryListBox.ItemsSource = NoRecordsCollection;

        if (SummaryCombobox.SelectedIndex == 2)
            MonthSummaryListBox.ItemsSource = NoRecordsCollection;
    }
}

在上面的功能中,数据是动态的。可以有两个,三个或更多记录也可以没有记录。我将此动态数据绑定到列表框中的文本块

我正在使用上面代码中使用的以下类

public class AmountCurrency
{
    public int Amount { get; set; }
    public String Currency { get; set; }

    public AmountCurrency(int Amount, String Currency)
    {
        this.Amount = Amount;
        this.Currency = Currency;
    }
}

我应该如何将上面两个textbock放在列表框中,这将显示项目的范围?能否请您提供我可以解决上述问题的任何代码或链接?如果我做错了什么,请指导我。

2 个答案:

答案 0 :(得分:9)

在我看来,你在模板的错误部分内有两个文本块。

这两个文本块应位于ListBox.ItemTemplate,而不是ListBox.ItemsPanelTemplate

<ListBox ItemsSource="{Binding Items}">
    <ListBox.ItemsPanel>
        <ItemsPanelTemplate>
            <StackPanel Orientation="Horizontal"></StackPanel>
        </ItemsPanelTemplate>
    </ListBox.ItemsPanel>
    <ListBox.ItemTemplate>
        <DataTemplate>
            <StackPanel Margin="0,0,0,17" Width="300" Orientation="Horizontal">
                <TextBlock TextWrapping="Wrap" Width="150" Text="{Binding Amount}" Foreground="Orange"></TextBlock>
                <TextBlock TextWrapping="Wrap" Width="150" Text="{Binding Currency}" Foreground="Orange"></TextBlock>
            </StackPanel>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>

如果您想了解此控件的工作原理,请从working "basic" control which shows items开始,然后查看将ItemsPanelTemplate添加到该工作示例中。

答案 1 :(得分:0)

DataContext="{Binding}"替换为适用于我的ItemsSource="{Binding}"