简体   繁体   中英

How to fill Combo box inside DataGrid?

I have two two classes like this (I'm giving short version):

public class Assistant: RegisteredUser, INotifyPropertyChanged
        private int institution;
        private int professor;

        public Assistant(int id, bool active, string name, string lastName, string email, string username, string password,  ScheduleBehavior scheduleBehavior, int professor, int institution)
            : base(id, active, name, lastName, email, username, password,  scheduleBehavior)
        {
            this.professor = professor;
            this.institution = institution;
        }

    public class Professor : RegisteredUser, INotifyPropertyChanged
           {

            private List<Assistant> assistants;
            private int institution;

            public Professor(int id, bool active, string name, string lastName, string email, string username, string password, ScheduleBehavior scheduleBehavior, List<Assistant> assistants, int institution)
                : base(id, active, name, lastName, email, username, password, scheduleBehavior)
            {
                this.assistants = assistants;
                this.institution = institution;
            }

Somewhere during inital load of MainWindow, I will save objects in two static hash maps like this:

public static Dictionary<int, Assistant> assistants = load("assistants");
public static Dictionary<int, Professor> professors = load("professors");

Keys are Id fields of objects themselves.

As instance variables in my window I have following:

private ICollectionView assistantsView;
private ICollectionView professorsView;
private ObservableCollection<Assistant > assistants;
private ObservableCollection<Professor> professors;

Assistant tabItem, called tabAssistans, which contains DataGrid (dgAssistants) is set up like this:

dgAssistants.DataContext = assistants;
assistantsView = CollectionViewSource.GetDefaultView(assistants);
dgAssistants.ItemsSource = assistantsView;
dgAssistants.IsSynchronizedWithCurrentItem = true
cbColumnProfessors.ItemsSource = professors;  //drop down list of available proffesors  

Xaml for dgAssistants:

<DataGrid x:Name="dgAssistants" Margin="-2,-8,-2,0" AutoGenerateColumns="False" IsReadOnly="False" CanUserAddRows="false">

  <DataGrid.Columns >

          <DataGridTextColumn Header="Name" Binding="{Binding Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"  Width="*"/>
           <DataGridTextColumn Header="LastName" Binding="{Binding LastName, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"  Width="*"/>
           <DataGridTextColumn Header="Email" Binding="{Binding Email, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"  Width="*"/>
           <DataGridTextColumn Header="Username" Binding="{Binding Username, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"  Width="*"/>
           <DataGridTextColumn Header="Password" Binding="{Binding Password, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"  Width="*"/>
           <DataGridComboBoxColumn x:Name="cbColumnProfessors" Header="Professor" Width="*" SelectedValueBinding="{Binding Professor, Mode=TwoWay}" DisplayMemberPath="Name" SelectedValuePath="Professor" >
                        <DataGridComboBoxColumn.EditingElementStyle>
                            <Style TargetType="{x:Type ComboBox}">
                                <EventSetter Event="SelectionChanged" Handler="yourCBSelectionChanged" />
                            </Style>
                        </DataGridComboBoxColumn.EditingElementStyle>
                    </DataGridComboBoxColumn>

    </DataGrid.Columns>
                </DataGrid>


 private void yourCBSelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            Assistant rowAssistant = (Assistant)dgAssistants.CurrentItem;
            ComboBox cmb = (ComboBox)sender;
            Professor selectedProfessor = professors[cmb.SelectedIndex];
            cmb.SelectedItem = selectedProfessor;
            dgAssistants.UpdateLayout();


            FileTextIO.Edit(rowAssistant, Assistant.PATH);

        }

I did manage with all this to populate data grid and to populate combo boxes (in each row) with Professors. Only that. I need a way to "bind" somehow that combo box and Assistant object of that row.

Problem is how to display names of professors in combo box and once I first time open window to set selectedItem of combo box to be the value of Name of Assistant's Professor. And if the value of Professor is -1 then combo box should be displaying nothing (like it is empty).

首次打开窗口时

The SelectedValueBinding should bind to a property of the Assistant class that stores the currently selected professor and the DisplayMemberPath property should be set to the name of a property of the Professor class. You also need to set the SelectedValuePath property to the name of property of the Professor class where you get the value to be selected from:

<DataGridComboBoxColumn x:Name="cbColumnProfessors" Header="Professor" Width="*"
                        SelectedValueBinding="{Binding Professor, Mode=TwoWay}"
                        DisplayMemberPath="Name"
                        SelectedValuePath="Id"  />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM