ssb
ssb

Reputation: 1472

Why isn't my WPF datagrid being populated?

I'm following the tutorial here: http://www.wpf-tutorial.com/datagrid-control/introduction/

I set up the DataGrid, created a collection with the data source, and set the DataGrid's ItemsSource to that collection. Upon running the program, it seems the number of rows is correct, but all the rows are blank. What do I need to change to get the data to show correctly?

xaml:

<Window x:Class="Task_Timer.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Task Timer" Height="284" Width="598" MinWidth="500" MinHeight="400">
    <Grid Margin="10">
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <DataGrid x:Name="ProcessBox" Margin="0" SelectionMode="Extended" ScrollViewer.HorizontalScrollBarVisibility="Hidden" GridLinesVisibility="None" AutoGenerateColumns="False" IsManipulationEnabled="True" IsReadOnly="True" HorizontalAlignment="Left" Width="285">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Process" Binding="{Binding ProcessName}" />
                <DataGridTextColumn Header="Memory" Binding="{Binding Memory}" />
            </DataGrid.Columns>
        </DataGrid>
        <Button Content="Button" Grid.Column="1" HorizontalAlignment="Left" Margin="10,0,0,0" VerticalAlignment="Top" Width="75"/>
    </Grid>
</Window>

C#:

namespace Task_Timer
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            List<ProcessContainer> items = new List<ProcessContainer>();
            foreach (Process process in Process.GetProcesses())
            {
                items.Add(new ProcessContainer(process.ProcessName, 10));
            }

            ProcessBox.ItemsSource = items;
        }
    }

    public class ProcessContainer
    {
        public string ProcessName;
        public int Memory;

        public ProcessContainer(string name, int memory)
        {
            ProcessName = name;
            Memory = memory;
        }
    }

}

Upvotes: 0

Views: 208

Answers (2)

Sajeetharan
Sajeetharan

Reputation: 222522

You are missing get and set. You need to set the value of your Properties inside the Class ProcessContainer

Change it like this,

public class ProcessContainer
        {
            public string ProcessName {get;set;}
            public int Memory { get; set; }

            public ProcessContainer(string name, int memory)
            {
                ProcessName = name;
                Memory = memory;
            }
        }

Upvotes: 3

Karuppasamy
Karuppasamy

Reputation: 216

change your ProcessContainer class as follows

 public class ProcessContainer
    {
        private string process;

        public string ProcessName
        {
            get { return process; }
            set { process = value; }
        }

        private int memory;

        public int Memory
        {
            get { return memory; }
            set { memory = value; }
        }


        public ProcessContainer(string name, int memory)
        {
            ProcessName = name;
            Memory = memory;
        }
    }

Upvotes: 0

Related Questions