S
S
SitrixEug2015-08-07 12:09:07
WPF
SitrixEug, 2015-08-07 12:09:07

Why is data not being bound to combobox in WPF DataGrid?

I use WPF and Entity Framework, following the MVVM pattern.
There is View - ConductorsView, connected with ConductorsViewModel.
On the Datagrid view, where the actual conductors are displayed. One of the columns needs to be made a combobox so that you can select values ​​from the list.
Here is an example ConductorsViewModel:

class ConductorsViewModel : WorkspaceViewModel, IDisposable
    {
        /// <summary>
        /// Контекст для работы с БД
        /// </summary>
        private PUTLEntities db;

        /// <summary>
        /// Коллекция оболочка для работы с кондукторами
        /// </summary>
        public ObservableCollection<Conductor> Conductors { get; set; }

        /// <summary>
        /// Коллекция оболочка Brigades
        /// </summary>
        public ObservableCollection<Brigade> Brigades { get; set; }

        /// <summary>
        /// Коллекция сообщений(лог)
        /// </summary>
        public ObservableCollection<string> Messages { get; private set; }



        #region Constructor

        public ConductorsViewModel()
        {
            Messages = new ObservableCollection<string>();
            base.DisplayName = Resources.ConductorsViewModel_DisplayName;
            db = (PUTLEntities)PutlDbProvider.GetDbContainer();

            Conductors = new ObservableCollection<Conductor>(db.Conductors);
            Brigades = new ObservableCollection<Brigade>(db.Brigades);

            Save = new DelegateCommand(this.SaveHandler);
        }

        #endregion // Constructor

And the actual XAML datagrid code:
<DataGrid ItemsSource="{Binding Path=Conductors}" AutoGenerateColumns="False" Margin="0,-8,0,8" Grid.ColumnSpan="5">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding CondNo}" Header="Номер"/>
                <DataGridTextColumn Binding="{Binding FIO}" Header="ФИО"/>
                <DataGridTextColumn Binding="{Binding DateHire}" Header="Дата приема"/>
                <DataGridTextColumn Binding="{Binding DateDismiss}" Header="Дата увольнения"/>
                <DataGridComboBoxColumn ItemsSource="{Binding Source=Brigades}" Header="Колона" Width="auto" SelectedItemBinding="{Binding FK_Brigade}" />
            </DataGrid.Columns>
        </DataGrid>

But he doesn't want to work. The combobox is empty, the values ​​in the column are empty. Tried all sorts of options, it doesn't work.
In what there can be a problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sumor, 2015-08-08
@SitrixEug

For the DataGridComboBoxColumn, the DataContext is a concrete object of type Conductor.
And the ItemsSource must take the list from somewhere. And apparently there is no such list in Conductor.
There are several options:
1 Connect to the DataGrid through the binding and take the list of Brigares from it
2 Define a static list of Brigades somewhere, for example in the ConductorsViewModel. Then refer to it via {x:Static local:ConductorsViewModel.Brigades}
3 Have a link to the Brigades list in each Conductor element and then just specify this property in the binding.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question