1

データ グリッドに従業員リストのデータを表示したいと考えています。以下のコードを実行すると、空のグリッドが表示されます。

XAML

<Window x:Class="SampleWpfApplication1.DataGridBindToEmployeeList"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="DataGridBindToEmployeeList" Height="300" Width="300" 
        xmlns:local="clr-namespace:SampleWpfApplication1.ViewModel">
    <Window.Resources>
        <local:EmployeeInfo x:Key="employeeInfo"/>
    </Window.Resources>
    <DataGrid ItemsSource="{Binding Source={StaticResource employeeInfo}}" AutoGenerateColumns="False" Height="300" Width="300" >
        <DataGridTextColumn Binding="{Binding Path=EmployeeId}" Header="Employee Id" Width="300"/>
        <DataGridTextColumn Binding="{Binding Path=EmployeeName}" Header="Employee Name" Width="300"/>
    </DataGrid>
</Window>

データコンテキスト:

public class Employee
    {
        public int EmployeeId { get; set; }
        public string EmployeeName { get; set; }
    }

    public class EmployeeInfo
    {
        public ObservableCollection<Employee> EmployeeList { get; set; }

        public EmployeeInfo()
        {
            EmployeeList = new ObservableCollection<Employee>();

            for (int i = 0; i < 3; ++i)
            {
                EmployeeList.Add(new Employee() { EmployeeId = i, EmployeeName = i.ToString() + "ABC" });
            }
        }
    }

出力は次のようになります。

Employee Id   | Employee Name
1             | 1ABC
2             | 2ABC
3             | 3ABC
4

1 に答える 1

0

これを試して:

<DataGrid ItemsSource="{Binding Source={StaticResource employeeInfo}, Path=EmployeeList}" AutoGenerateColumns="False" Height="300" Width="300" >
   <DataGrid.Columns>
      <DataGridTextColumn Binding="{Binding Path=EmployeeId}" Header="Employee Id" Width="300"/>
      <DataGridTextColumn Binding="{Binding Path=EmployeeName}" Header="Employee Name" Width="300"/>
   </DataGrid.Columns>
</DataGrid>

ItemsSourceクラス全体に設定しEmloyeeInfo、あなたのを指したい瞬間にObservableCollection<Employee>、またあなたの列をラップするのを忘れましたDataGrid.Columns

于 2013-08-22T12:57:58.023 に答える