简体   繁体   中英

How can I fill a listbox like this with data

What's the c# code to fill these listbox with datas (strings)

I saw this on https://docs.microsoft.com/en-us/dotnet/framework/wpf/data/data-templating-overview

but there's no C# Code.

I want to have a listbox like in "Defining a Simple DataTemplate" on the link https://docs.microsoft.com/en-us/dotnet/framework/wpf/data/data-templating-overview

Picture from listbox [1]: https://i.stack.imgur.com/K4HZS.png

<ListBox Width="400" Margin="10"
         ItemsSource="{Binding Source={StaticResource myTodoList}}">
   <ListBox.ItemTemplate>
     <DataTemplate>
       <StackPanel>
         <TextBlock Text="{Binding Path=TaskName}" />
         <TextBlock Text="{Binding Path=Description}"/>
         <TextBlock Text="{Binding Path=Priority}"/>
       </StackPanel>
     </DataTemplate>
   </ListBox.ItemTemplate>
 </ListBox>

To do this, you will need to learn about MVVM pattern. First, you need a class TODO in the model with properties for TaskName, Description and Priority.

public class Todo
{
    public string TaskName { get; set; }
    public string Description { get; set; }
    public int Priority { get; set; }
}

Then, you will need a class to store the collection of TODO, your "myTodoList" in the xaml :

public class TodoViewModel
{
    public ObservableCollection<Todo> TodoList { get; } = new ObservableCollection<Todo>();

    public TodoViewModel()
    {
        TodoList.Add(new Todo { TaskName = "Todo1", Description = "Todo 1 Description", Priority = 1 });
        TodoList.Add(new Todo { TaskName = "Todo2", Description = "Todo 2 Description", Priority = 2 });
    }
}

Then, you will need to set the datacontext in the code behind of the xaml :

public partial class MainWindow : Window
{
    public MainWindow()
    {
        this.DataContext = new TodoViewModel();
        InitializeComponent();
    }
}

Finally, here is your XAML (I changed it a bit, you don't need things like "Path=") :

<ListBox Width="400" Margin="10" ItemsSource="{Binding TodoList}">
        <ListBox.ItemTemplate>
            <DataTemplate>
                <StackPanel Orientation="Horizontal">
                    <TextBlock Text="{Binding TaskName}" />
                    <TextBlock Text="{Binding Description}"/>
                    <TextBlock Text="{Binding Priority}"/>
                </StackPanel>
            </DataTemplate>
        </ListBox.ItemTemplate>
</ListBox>

And it should work well :)

You should have something like this.

MainWindow.xaml.cs:

public List<ToDo> ToDoList {get; set;}

public MainWindow()
{
    InitializeComponent();
    DataContext=this;
    ToDoList= new List<ToDo>()
        {
            new ToDo{TaskName="Task1",Description="First Task"},
            new ToDo{TaskName="Task2",Description="Second Task"}
        };
}

ToDo.cs:

public class ToDo
{
    public string TaskName {get; set;}
    public string Description {get; set;}
}

Modify your Binding expression to:

<ListBox Width="400" Margin="10"
     ItemsSource="{Binding ToDoList}">

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM