A
A
Anna Panova2021-07-04 19:01:46
WPF
Anna Panova, 2021-07-04 19:01:46

How to display a list of class objects in a WPF ListBox?

Hello! Please tell me how to do it. Given a class consisting of two fields (desire and the percentage of its implementation). A list is created from class objects. And you need to fill in the ListBox by accessing the fields of these objects.
And where completion should be ProgressBar. Now I just see 3 ProgressBars with a length of 100 and nothing else
Code MainWindow.xaml

<StackPanel>
        <ListBox x:Name="MyLB">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition/>
                            <ColumnDefinition Width="10"/>
                            <ColumnDefinition Width="100"/>
                        </Grid.ColumnDefinitions>
                        <TextBlock Text="{Binding title}" Grid.Column="0"/>
                        <ProgressBar Minimum="0" Maximum="100" Value="{Binding Completion}" Grid.Column="2"/>
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </StackPanel>


Class Code
class Wish1
    {
        public string title;
        public int Completion;
    }


MainWindow.xaml.cs code
public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.Loaded += MainWindow_Loaded;
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
               
            MyLB.ItemsSource = new[] {
                new Wish1() { title = "перейти на 2 курс", Completion = 100},
            new Wish1() { title = "новый ноут", Completion = 50 },
            new Wish1() { title = "познать в чем смысл жизни", Completion = 0 }}; 
        }
    }

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question