I
m having troubles with displaying data from table. The table has one field ``PositionName`` and build in Entity Framework Code First. For displaying I
<DataGrid AutoGenerateColumns="False"
x:Name="PositionGrid" Margin="2 2 2 2"
ItemsSource="{Binding Source = PositionCollection}"
SelectedItem="{Binding ThePosition, Mode=TwoWay}"
SelectionMode="Single" Height="235" >
<DataGrid.Columns >
<DataGridTextColumn Binding="{Binding PositionName}"
Header="Назва професії"/>
</DataGrid.Columns>
</DataGrid>
public AddPositionView()
{ InitializeComponent();
DataContext = new PositionViewModel();
}
public PositionViewModel()
{
_db = new SalDBContext();
ThePosition = new Position();
_poclog = new PositionLogic();
PositionCollection = new ObservableCollection<Position>
(_poclog.Get());
}
internal IEnumerable<Position> Get()
{
return _dbContext.Positions.ToList();
}
private void PositionShow_Button_Click(object sender, RoutedEventArgs e)
{
AddPositionView posview = new AddPositionView();
posview.Show();
}
PositionCollection
must be a public property for you to be able to bind to it:
public PositionCollection { get; private set; }
You should also set the Path
of the Binding
(and not the Source
) to the name of the property:
ItemsSource="{Binding Path=PositionCollection}"