WPF C# Data-binding to DataGridTextColumn

DisplayName13579 picture DisplayName13579 · Sep 29, 2013 · Viewed 42.2k times · Source

I'm having a difficult time getting any data in this datagrid view to appear. I've followed some suggestions in a few other StackOverflow forum posts but am not having any luck getting the content to appear.

<DataGrid 
    x:Name="DataGridEmployees"
    DataContext="{Binding RelativeSource={RelativeSource AncestorType=Window}}"
    ItemsSource="{Binding GridView}"
    AutoGenerateColumns="True"
    Loaded="dataGrid1_Loaded"
    Margin="0,2,0,-2" Grid.ColumnSpan="2">
    <DataGrid.Columns>
        <DataGridTextColumn Header="EmployeeId" Width="175" Binding="{Binding Id}"></DataGridTextColumn>
        <DataGridTextColumn Header="Title" Width="175" Binding="{Binding Title}"></DataGridTextColumn>
        <DataGridTextColumn Header="WorkStatus" Width="175" Binding="{Binding WorkStatus}"></DataGridTextColumn>
        <DataGridTextColumn Header="FullName" Width="175" Binding="{Binding FullName}"></DataGridTextColumn>
    </DataGrid.Columns>
</DataGrid>

This is the click event that is being fired from a separate window (? could this be causing any problems?) in the xaml.cs file

public partial class MainMenu : Window
{
    WpfSampleEntities2 _context = new WpfSampleEntities2();

    public MainMenu()
    {
        InitializeComponent();
    }

    private void Button_Click_1(object sender, RoutedEventArgs e)
    {
        EmployeeDetails ed = new EmployeeDetails();
        ed.DataContext = ed.DomainEmployees;
        Binding bin = new Binding("GridView");
        bin.Source = ed.DomainEmployees;

        foreach (var item in ed.DomainEmployees)
        {
            bin.Path.PathParameters.Add(item);
        }

        ed.Show();
    }

}

And here is the EmployeeDetails.cs class/vm:

[TypeConverter(typeof(DataGridTextColumn))]
public class MVVMEmployee : Employee
{
    public int Id { get; set; }
    public string FullName { get; set; }
    public string Title { get; set; }
    public string WorkStatus { get; set; }

    public MVVMEmployee() { }
    public MVVMEmployee(int id, string fullName, string title, string workStatus)
    {
        this.Id = id;
        this.FullName = fullName;
        this.Title = title;
        this.WorkStatus = workStatus;
    }
}

I've also tried with the XAML as:

<DataGrid
    x:Name="DataGridEmployees"
    DataContext="{Binding RelativeSource={RelativeSource AncestorType=Window}}"
    ItemsSource="{Binding GridView}"
    AutoGenerateColumns="True"
    Loaded="dataGrid1_Loaded"
    Margin="0,2,0,-2" Grid.ColumnSpan="2">
    <DataGrid.Columns>
        <DataGridTextColumn Header="EmployeeId" Width="175" Binding="{Binding ElementName=Id}" ></DataGridTextColumn>
        <DataGridTextColumn Header="Title" Width="175" Binding="{Binding ElementName=Title}"></DataGridTextColumn>
        <DataGridTextColumn Header="WorkStatus" Width="175" Binding="{Binding ElementName=WorkStatus}"></DataGridTextColumn>
        <DataGridTextColumn Header="FullName" Width="175" Binding="{Binding ElementName=FullName}"></DataGridTextColumn>
    </DataGrid.Columns>
</DataGrid>

Answer

user1656632 picture user1656632 · Sep 29, 2013
myWindow w = new myWindow();
         w.DataContext = myViewModel;
         w.Show();

DataContext="{Binding RelativeSource={RelativeSource AncestorType=Window}}"

OR

<Window x:Name="MyWindow" />
    //DataGrid
    {Binding DataContext, ElementName=MyWindow}
    ItemsSource="{Binding MyViewModel.MyList}"