发布于 2024-11-07 02:30:23 字数 2158 浏览 4 评论 0原文

我有一个绑定到数据表的数据网格,带有 ComboBoxColumn。该列的 XAML 如下:

<DataGridComboBoxColumn Header="Rep Name" SortMemberPath="RepName" 
                      ItemsSource="{Binding UpdateSourceTrigger=PropertyChanged, Source={StaticResource EmployeeList}, Path=Employees}"
                      SelectedValueBinding="{Binding Mode=TwoWay, Path=EmpId}"
                      SelectedValuePath="EmpId" DisplayMemberPath="RepName" />


我的员工类:

public class EmployeeList : INotifyPropertyChanged
    {
        private ObservableCollection<Employee> _employees = new ObservableCollection<Employee>();

        public EmployeeList()
        {
           ...
        }

        public ObservableCollection<Employee> Employees
        {
            get { return _employees; }
            set { _employees = value; NotifyPropertyChanged("Employees"); }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

    public class Employee : INotifyPropertyChanged
    {
        private int _id;
        public int EmpId
        {
            get { return _id; }
            set { _id = value; OnPropertyChanged("EmpId"); }
        }

        public string RepName { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this,
                    new System.ComponentModel.PropertyChangedEventArgs(propertyName));
        }
    }


作为网格项目源的 DataTable 包含“EmpId”列和“RepName”列。组合框填充了我的所有员工,当我做出选择时,它会反映在数据表中。但是,当屏幕加载时,默认情况下不会在组合框中选择当前分配的员工。我认为组合框的 SelectedValueBinding 属性可以处理这个问题...我做错了什么?


更新澄清:
数据网格绑定到包含 EmployeeID 列的数据表。假设屏幕加载时,表中有三行,其中 EmployeeID 为 1、2 和 3。我需要第一行中的组合框列选择 EmployeeID 1,第二行选择 EmployeeID 2,然后第三行选择 EmployeeID 3。

I've got a datagrid bound to a datatable, with a ComboBoxColumn. The XAML for this column is as follows:

<DataGridComboBoxColumn Header="Rep Name" SortMemberPath="RepName" 
                      ItemsSource="{Binding UpdateSourceTrigger=PropertyChanged, Source={StaticResource EmployeeList}, Path=Employees}"
                      SelectedValueBinding="{Binding Mode=TwoWay, Path=EmpId}"
                      SelectedValuePath="EmpId" DisplayMemberPath="RepName" />

My Employees class:

public class EmployeeList : INotifyPropertyChanged
    {
        private ObservableCollection<Employee> _employees = new ObservableCollection<Employee>();

        public EmployeeList()
        {
           ...
        }

        public ObservableCollection<Employee> Employees
        {
            get { return _employees; }
            set { _employees = value; NotifyPropertyChanged("Employees"); }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void NotifyPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

    public class Employee : INotifyPropertyChanged
    {
        private int _id;
        public int EmpId
        {
            get { return _id; }
            set { _id = value; OnPropertyChanged("EmpId"); }
        }

        public string RepName { get; set; }

        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this,
                    new System.ComponentModel.PropertyChangedEventArgs(propertyName));
        }
    }

The DataTable serving as items source for the grid contains an "EmpId" column and a "RepName" column. The combobox is populated with all my employees, and when I make a selection, it is reflected in the datatable. However, when the screen loads, the currently assigned employee is not selected by default in the combobox. I thought that the SelectedValueBinding property of the combobox would handle this... what am I doing wrong?

Update for clarification:

The datagrid is bound to a datatable which includes an EmployeeID column. Let's assume that when the screen loads, there are three rows in the table with EmployeeIDs 1, 2, and 3. I need the combobox column in the first row to have EmployeeID 1 selected, the second row to have EmployeeID 2 selected, and the third row to have EmployeeID 3 selected.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

呆头 2024-11-14 02:30:23

如果我正确理解您的问题,ComboBox 默认情况下不会选择集合中的第一项?

如果是这种情况,您应该将 ComboBox 上的属性 IsSychronizedWithCurrentItem 设置为 true。有关 Selector.IsSynchronizedWithCurrentItem 的信息 这里

...

嗯,不幸的是,经过进一步研究,DataGridComboBoxColumn 没有 IsSynchronizedWithCurrentItem 属性。 :/但是,您可以创建一个看起来像这样的DataGridTemplateColunm

<DataGridTemplateColumn Header="Rep Name">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <ComboBox ItemsSource="{Binding Employees}" 
                   IsSynchronizedWithCurrentItem="true" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColunm>

希望有帮助!

If I understand your problem correctly, the ComboBox doesn't select the first item in the collection by default?

If that's the case, you should set the property IsSychronizedWithCurrentItem on the ComboBox to true. Info about Selector.IsSynchronizedWithCurrentItem here.

...

Well, unfortunately upon further research, DataGridComboBoxColumn does not have a IsSynchronizedWithCurrentItem property. :/ But, you can create a DataGridTemplateColunm that looks something like this:

<DataGridTemplateColumn Header="Rep Name">
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
            <ComboBox ItemsSource="{Binding Employees}" 
                   IsSynchronizedWithCurrentItem="true" />
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColunm>

Hope that helps!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文