Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将自动生成的网格转换为具有单个列的网格_C#_Wpf_Datagrid_Multiple Columns - Fatal编程技术网

C# 如何将自动生成的网格转换为具有单个列的网格

C# 如何将自动生成的网格转换为具有单个列的网格,c#,wpf,datagrid,multiple-columns,C#,Wpf,Datagrid,Multiple Columns,实际上,我使用的是自动生成的网格函数,如下所示: <DataGrid ItemsSource="{Binding UserDataObject}" Width="786" /> 现在我想指定各个列,如 <DataGrid> <Column Header="ID" /> <Column Header="Username" /> <Column Header="Role">--Show Role As Select

实际上,我使用的是自动生成的网格函数,如下所示:

<DataGrid ItemsSource="{Binding UserDataObject}" Width="786" />
现在我想指定各个列,如

<DataGrid>
    <Column Header="ID" />
    <Column Header="Username" />
    <Column Header="Role">--Show Role As Selector--</Column>
</DataGrid>
这在WPF中是如何工作的

说明:

我的UserDataObject定义为

ObservableCollection<User> mUserDataObject = new ObservableCollection<User>();

public ObservableCollection<User> UserDataObject
    {
        get
        {
            return mUserDataObject;
        }
    }

    //User user = new User("ID", "Username", "Password", "Role");

    public class User : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        // Attributes
        private int? _ID;
        private string _Username;
        private string _Password;
        private string _Role;

        // Constructors
        public User()
        {
        }

        public User(int pID, string pUsername, string pPassword, string pRole)
        {
            this.ID = pID;
            this.Username = pUsername;
            this.Password = pPassword;
            this.Role = pRole;
        }

        // Getter and Setter
        public int? ID
        {
            get { return _ID; }
            set { _ID = value; OnPropertyChanged("ID"); }
        }

        public string Username
        {
            get { return _Username; }
            set { _Username = value; OnPropertyChanged("Username"); }
        }

        public string Password
        {
            get { return _Password; }
            set { _Password = value; OnPropertyChanged("Password"); }
        }

        public string Role
        {
            get { return _Role; }
            set { _Role = value; OnPropertyChanged("Role"); }
        }

        // Create the OnPropertyChanged method to raise the event
        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }
    }
此外,如何添加在一个单元格中注册更改的事件侦听器?

您必须在dataGrid上将AutoGenerateColumns设置为False,并提供自己的列列表

<DataGrid AutoGenerateColumns="False" ItemsSource="{Binding UserDataObject}">
  <DataGrid.Columns>
    <DataGridTextColumn Header="ID" Binding="{Binding ID}"/>
    <DataGridTextColumn Header="Username" Binding="{Binding Username}"/>
    <DataGridComboBoxColumn Header="Role" ItemsSource="{Binding Role}"/>
  </DataGrid.Columns>
</DataGrid>
如果希望用户编辑属性,请使用DataGridTextColumn;如果希望用户仅从属性的允许值中选择值,请使用DataGridComboxColumn

如何添加在一个单元格中注册更改的事件侦听器

因为您有适当的绑定,所以不必担心在代码隐藏中处理它。将代码移动到单元格绑定到的属性设置器


不过,如果您感兴趣,可以挂起CellEditEnding事件。

尝试您的方法时,我的网格将完全变空。我将Binding={Binding ID}更改为Binding={Binding UserDataObject/ID},这也失败了…基础源类中是否存在ID属性?是的,它存在。我的ObservableCollection拥有一个具有属性ID的用户对象。您必须像这样设置ItemSource-。早些时候,我错过了在答案中更新的设置ItemsSource。这行吗?角色属性的类型也是String。所以您必须将第3列声明为DataGridTextColumn,如下-。您之前发布的帖子希望它是选择器类型,因此我假设您的角色属性是IEnumerable类型。我编辑了您的标题以删除标记。看看这背后的原因。