在Devexpress GridColumn中使用ComboBoxEditSettings

时间:2014-06-07 12:26:52

标签: c# wpf devexpress gridcontrol

我有User和UserGroup的OneToMeny关系对象模型,如下所示:

public class UserGroup : BaseModel, IEquatable<UserGroup>
{
    private long _userGroupId;
    private string _userGroupName;

    public long UserGroupId
    {
        get { return _userGroupId; }
        set
        {
            _userGroupId = value;
            NotifyOfPropertyChange(() => UserGroupId);
        }
    }

    public string UserGroupName
    {
        get { return _userGroupName; }
        set
        {
            _userGroupName = value;
            NotifyOfPropertyChange(() => UserGroupName);
        }
    }

    public UserGroup GetCopy(bool getNew)
    {
        var copy = new UserGroup
        {
            UserGroupId = UserGroupId,
            UserGroupName = UserGroupName,
            State = getNew ? ModelStates.New : State,
        };

        return copy;
    }

    public bool Equals(UserGroup other)
    {
        if (ReferenceEquals(null, other)) return false;
        if (ReferenceEquals(this, other)) return true;
        return _userGroupId == other._userGroupId;
    }

    public override bool Equals(object obj)
    {
        if (ReferenceEquals(null, obj)) return false;
        if (ReferenceEquals(this, obj)) return true;
        if (obj.GetType() != this.GetType()) return false;
        return Equals((UserGroup) obj);
    }

    public override int GetHashCode()
    {
        return _userGroupId.GetHashCode();
    }
}

public class User : BaseModel
{
    private long _userId;
    private string _userName;
    private string _password;
    private string _firstName;
    private string _lastName;
    private List<UserGroup> _userGroups;

    public User()
    {
        _userGroups = new List<UserGroup>();
    }

    public long UserId
    {
        get { return _userId; }
        set
        {
            _userId = value;
            NotifyOfPropertyChange(() => UserId);
        }
    }

    public string UserName
    {
        get { return _userName; }
        set
        {
            _userName = value;
            NotifyOfPropertyChange(() => UserName);
        }
    }

    public string Password
    {
        get { return _password; }
        set
        {
            _password = value;
            NotifyOfPropertyChange(() => Password);
        }
    }

    public string FirstName
    {
        get { return _firstName; }
        set
        {
            _firstName = value;
            NotifyOfPropertyChange(() => FirstName);
        }
    }

    public string LastName
    {
        get { return _lastName; }
        set
        {
            _lastName = value;
            NotifyOfPropertyChange(() => LastName);
        }
    }

    public List<UserGroup> UserGroups
    {
        get { return _userGroups; }
        set
        {
            _userGroups = value;
            NotifyOfPropertyChange(() => UserGroups);
        }
    }
}

我将使用Devexpress GridControl来保存和加载用户模型。所以我有一个像这样的GridControl:

    <dxg:GridControl ItemsSource="{Binding Users}" >
        <dxg:GridControl.Columns>
            <dxg:GridColumn x:Name="FirstName"
                            Width="10"
                            FieldName="FirstName" />
            <dxg:GridColumn x:Name="LastName"
                            Width="10"
                            FieldName="LastName" />
            <dxg:GridColumn x:Name="UserName"
                            Width="10"
                            FieldName="UserName" />
            <dxg:GridColumn x:Name="Password"
                            Width="10"
                            FieldName="Password">
                <dxg:GridColumn.EditSettings>
                    <dxe:PasswordBoxEditSettings />
                </dxg:GridColumn.EditSettings>
            </dxg:GridColumn>
            <dxg:GridColumn x:Name="UserGroups"
                            Width="15"
                            FieldName="UserGroups">
                <dxg:GridColumn.EditSettings>
                    <dxe:ComboBoxEditSettings ItemsSource="{Binding UserGroups}"
                                              DisplayMember="UserGroupName" >
                        <dxe:ComboBoxEditSettings.StyleSettings>
                            <dxe:CheckedComboBoxStyleSettings />
                        </dxe:ComboBoxEditSettings.StyleSettings>
                    </dxe:ComboBoxEditSettings>
                </dxg:GridColumn.EditSettings>
            </dxg:GridColumn>
        </dxg:GridControl.Columns>
        <dxg:GridControl.View>
            <dxg:TableView x:Name="TableViewAllEmployees"
                           AutoWidth="True"
                           NewItemRowPosition="Top"
                           ShowGroupPanel="False" />
        </dxg:GridControl.View>
    </dxg:GridControl>

不幸的是,运行时组合框在readonly模式下打开,我无法从中选择任何记录。 请告诉我如何启用组合框。

另外,我在WPF项目中使用Devexpress,Caliburn。

1 个答案:

答案 0 :(得分:0)

当指定的FieldName列无效(并且无法绑定)或列的绑定属性(FieldName)的数据类型与从列表中选择的项目的数据类型不一致时,我遇到了相同的效果。

您也可以尝试将ComboBoxEditSettings ValueMember设置为“UserGroupId”(我之前没有尝试将列绑定到项目列表,所以我不确定这一点。)