为什么我的WPF数据网格没有被填充?

时间:2014-07-20 07:31:04

标签: c# wpf datagridview

我在这里关注教程:http://www.wpf-tutorial.com/datagrid-control/introduction/

我设置了DataGrid,使用数据源创建了一个集合,并将DataGrid的ItemsSource设置为该集合。在运行程序时,似乎行数是正确的,但所有行都是空白的。我需要更改什么才能正确显示数据?

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

}

2 个答案:

答案 0 :(得分:3)

您缺少获取设置。您需要设置 ProcessContainer

中的属性值

像这样改变,

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

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

答案 1 :(得分:0)

按如下所示更改ProcessContainer类

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