0

I've created a code-first C# project with Entity Framework and WPF. I have created an Entity named Personel Entity. I'm dragging and dropping that Entity to MainWindow but it doesn't show any data. I think I have to do something in MainWindow.xaml.cs file but I don't know what to do. Here is the DataGrid code in xaml:

<DataGrid x:Name="personelEntityDataGrid" AutoGenerateColumns="False" EnableRowVirtualization="True" ItemsSource="{Binding}" Margin="19,259,18,10" RowDetailsVisibilityMode="VisibleWhenSelected">
    <DataGrid.Columns>
        <DataGridTextColumn x:Name="addressColumn" Binding="{Binding Address}" Header="Address" Width="SizeToHeader"/>
        <DataGridTextColumn x:Name="ageColumn" Binding="{Binding Age}" Header="Age" Width="SizeToHeader"/>
        <DataGridTextColumn x:Name="idColumn" Binding="{Binding Id}" Header="Id" Width="SizeToHeader"/>
        <DataGridTextColumn x:Name="nameColumn" Binding="{Binding Name}" Header="Name" Width="SizeToHeader"/>
        <DataGridTextColumn x:Name="phoneNumberColumn" Binding="{Binding PhoneNumber}" Header="Phone Number" Width="SizeToHeader"/>
    </DataGrid.Columns>
</DataGrid>

Here is the code in MainWindow.xaml.cs file :

public partial class MainWindow : Window
{
    private PersonelContext _context = new PersonelContext();

    public MainWindow()
    {
        InitializeComponent();
    }

    private void Window_Loaded(object sender, RoutedEventArgs e)
    {
        System.Windows.Data.CollectionViewSource personelEntityViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("personelEntityViewSource")));    
    }
}

Here is the code in PersonelContext.cs file :

namespace Personel
{
    public class PersonelContext : DbContext
    {
        public DbSet<PersonelEntity> Personels { get; set; }
    }
}

There is nothing else about datagrid in code. I know I need to add something but I don't know what to add. Can you tell me what to do?

Disposer
  • 6,201
  • 4
  • 31
  • 38
jason
  • 6,962
  • 36
  • 117
  • 198

2 Answers2

1

Basically every binding is built on a DataContext of particular FrameworkElement. In your case it is DataGrid. Data is not updated because you've not initialized data context for UseControl.
Please do not confuse it with Entity Frameworks DbContext which has nothing to do with controls DataContext.

So to make your screen working just add following line into Window_Loaded method:

this.DataContext = _context.Personels.ToList();
Anatolii Gabuza
  • 6,184
  • 2
  • 36
  • 54
1

Make one Change in Xaml as below

ItemSource= {Binding}

to 

ItemSource= {Binding Path=.}

and in Code behind

personelEntityDataGrid.ItemSource =_context.Personels.ToList();

(if above not works try OR part).

or

personelEntityDataGrid.DataContext =_context.Personels.ToList();
Ashok Rathod
  • 840
  • 9
  • 24
  • It worked. Thank you. Can you tell me what ItemSource= {Binding Path=.} does? – jason Jun 27 '14 at 13:59
  • 1
    In Binding Mechanism, You had to set source property of your binded Collection(e.g DataTablem,List) and here Path=. Means root element of the Collection. As u can see in code behind we had assigned DataContext of personelEntityDataGrid(Actual element). So Here what it consider by Binding Path=. Here DataGrid will be bind with the root element(List element in this case).In short, . indicates root element of the binded collection. u can also consider below think for better understanding http://stackoverflow.com/questions/1066262/special-symbols-in-wpf-binding-what-does-binding-path-mean – Ashok Rathod Jun 30 '14 at 04:53