水平显示多个数据

时间:2009-08-03 11:26:12

标签: c# silverlight silverlight-2.0

如何在列表框中水平显示数据。我曾使用属性从文本框中读取数据,我必须将这些多个数据显示在列表框中(水平地在一行中)。我的代码如下所示..

private void SaveButton_Click(object sender, RoutedEventArgs e)
    {
        LoadData();



    }

private void LoadData()
    {
        List<ItemList> MyList = new List<ItemList>();
        MyList.Add(new ItemList { Subject = subjectText.Text });
        MyList.Add(new ItemList { Createdtext = CreatedText.Text });
        MyList.Add(new ItemList { Calendartext = CalendarText.Text });
        MyList.Add(new ItemList { TimeText = TimeText.Text });
        MyList.Add(new ItemList { AssignedText = AssignedText.Text });
        MyList.Add(new ItemList { DescriptionText = DescriptText.Text });
        MyList.Add(new ItemList { TargetdateText = TargetDateText.Text });

        MyListBox.ItemsSource = MyList;

    }

获取数据的属性是:

public class ItemList
{
    private string _subject;
    private string _createdtext;
    private string _calendartext;
    private string _timeText;
    private string _assignedText;
    private string _descriptionText;
    private string _targetdateText;


    public string Subject
    {
        get
        {
            return _subject;
        }
        set
        {
            _subject = value;
        }
    }

    public string Createdtext
    {
        get
        {
            return _createdtext;
        }
        set
        {
            _createdtext = value;
        }
    }

    public string Calendartext
    {
        get
        {
            return _calendartext;
        }

        set
        {
            _calendartext = value;
        }
    }

    public string TimeText
    {
        get
        {
            return _timeText;
        }

        set
        {
            _timeText = value;
        }
    }

    public string AssignedText
    {
        get
        {
            return _assignedText;
        }

        set
        {
            _assignedText = value;
        }
    }

    public string DescriptionText
    {
        get
        {
            return _descriptionText;
        }

        set
        {
            _descriptionText = value;
        }
    }

    public string TargetdateText
    {
        get
        {
            return _targetdateText;
        }

        set
        {
            _targetdateText = value;
        }
    }
}

列表框的我的XAML代码如下所示

<ListBox x:Name="MyListBox" ItemsSource="ItemList" Grid.ColumnSpan="6" HorizontalAlignment="Left"  Grid.Column="1" Grid.Row="8" Width="600" Height="96">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel  Orientation="Horizontal" >
                    <Grid ShowGridLines="True" Width="700">
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="100" />
                            <ColumnDefinition Width="100" />
                            <ColumnDefinition Width="100" />
                            <ColumnDefinition Width="100" />
                            <ColumnDefinition Width="100" />
                            <ColumnDefinition Width="100" />
                            <ColumnDefinition Width="100" />
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="25" />

                        </Grid.RowDefinitions>

                     <TextBlock Text="{Binding Subject}" Grid.Column="0"/>
                    <TextBlock Text="{Binding Createdtext}"  Grid.Column="1" />
                    <TextBlock Text="{Binding Calendartext}" Grid.Column="2" />
                    <TextBlock Text="{Binding Timetext}" Grid.Column="3"/>
                    <TextBlock Text="{Binding AssignedText}"  Grid.Column="4"/>
                    <TextBlock Text="{Binding DescriptionText}"  Grid.Column="5" />
                    <TextBlock Text="{Binding TargetdateText}"  Grid.Column="6"/>
                    </Grid>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>

但我没有得到一行输出。它以对角线显示..获得正确输出所需的更改是什么..

2 个答案:

答案 0 :(得分:2)

<ListBox x:Name="MyListBox" ItemsSource="ItemList" Grid.ColumnSpan="6" HorizontalAlignment="Left"  Grid.Column="1" Grid.Row="8" Width="600" Height="96">
...
        <ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Horizontal"/>
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>
...
</ListBox>

试试这个

答案 1 :(得分:0)

查看MSDN page for the ItemsPanel property。它有一个演示如何创建水平列表框的示例。