【问题标题】:How to fill Combo box inside DataGrid?如何在 DataGrid 中填充组合框?
【发布时间】:2020-03-14 08:48:57
【问题描述】:

我有两个这样的类(我给出的是简短版本):

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;
            }

在 MainWindow 的初始加载期间,我会将对象保存在两个静态哈希映射中,如下所示:

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

键是对象本身的 Id 字段。

作为我窗口中的实例变量,我有以下内容:

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

Assistant tabItem,叫做tabAssistans,里面包含DataGrid(dgAssistants)是这样设置的:

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

dgAssistants 的 Xaml:

<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);

        }

我确实管理了所有这些来填充数据网格并用教授填充组合框(在每一行中)。只有这样。我需要一种方法以某种方式“绑定”该行的组合框和助手对象。

问题是如何在组合框中显示教授的姓名,一旦我第一次打开窗口将组合框​​的 selectedItem 设置为助理教授姓名的值。如果 Professor 的值为 -1,那么组合框应该什么也不显示(就像它是空的一样)。

【问题讨论】:

  • Assistant 类是否具有某种“SelectedProfessor”属性?它应该(或必须)让您能够将当前选定的教授存储在某个地方。
  • @mm8 哦,是的,它有(教授)。所有属性都在那里,以及具有“属性已更改”的设置器...

标签: c# wpf combobox datagrid


【解决方案1】:

SelectedValueBinding 应绑定到存储当前选定教授的Assistant 类的属性,并且DisplayMemberPath 属性应设置为@987654324 的属性的名称 @ 班级。您还需要将SelectedValuePath 属性设置为Professor 类的属性名称,您可以从中获取要从中选择的值:

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

【讨论】:

  • 感谢mm8重播。是的,您的代码有效,组合框中填充了教授及其姓名。现在,我不知道你有没有注意到,Assistant class 中的 field Professor 是一个 int。如何在组合框中为 dgAssistants 中的每一行(即每个助理)设置该助理教授的姓名。我不确定我是否可以理解地解释这一点:)
  • @MiljanPuletic:将SelectedValuePath 属性设置为Professor 类中对应int 属性的名称。查看我的编辑。
  • 我正在尝试使用 IValueConverter,但效果不佳..
  • 您好,先生,按照您的说法,我又尝试了一次,但不幸的是,我没有得到想要的结果。每个组合框都已设置为教授列表中的第一个值,一旦我从组合框中进行任何选择并按 Enter,组合框中的值将返回列表中的第一个值。我做了编辑,所以你可以看到。
  • 如果你在Professor的setter中设置断点,它会被命中吗?它的值是多少?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-16
  • 1970-01-01
  • 1970-01-01
  • 2015-03-23
  • 1970-01-01
  • 2013-04-28
相关资源
最近更新 更多