水平显示多个数据

发布于 2024-07-29 08:30:46 字数 4359 浏览 3 评论 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>

但我没有在一行中获得输出。 它以对角线显示..获得正确输出需要进行哪些更改..

how to display data horizontally in a list box. I had used the properties for reading data from the text box and i have to display these multiple data in to a list box (in one line, horizontally) . my code is shown below..

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;

    }

The property for getting data is:

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

My XAML code for List box is shown below

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

but i didn't get the output in one line. it displayed in diagonally.. What are the changes required for getting correct output..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

凌乱心跳 2024-08-05 08:30:46
<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>

尝试这个

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

try this

拥抱没勇气 2024-08-05 08:30:46

查看 ItemsPanel 属性 的 MSDN 页面。 它有一个示例演示如何创建水平列表框。

Have a look at the MSDN page for the ItemsPanel property. It has an example demonstrating how to create a horizontal list box.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文