根据多个用户条件启用按钮

时间:2014-04-25 17:50:28

标签: c# wpf if-statement user-controls conditional-statements

我有一个名为DisabledControl的用户控件。我需要使用这两个条件创建1 if语句。

public static class DisabledControl
{
    public static void AdminOnly(this Control control)
    {
        control.Enabled = User.IsInRole(new string[] { "Administrator" });
    }

    public static void OwnerOnly(this Control control, int ownerID)
    {
        control.Enabled = (User.ID == ownerID);
    }
}

//I need help with syntax: if User isinRole 'Adminstrator" && user is OwnerOnly, btnSave.Enabled=true. 
//I tried to use if user.id == OwnerOnly, it gives error.

1 个答案:

答案 0 :(得分:1)

您无法user.id == OwnerOnly,因为OwnerOnly是一种返回void的方法。您需要user.id == ownerID,或更改OwnerOnly才能返回boolean

回应您的评论,请尝试以下方式:

public static void SetEnabledForAdminOrOwner(this Control control, int ownerID)
{
    control.Enabled = User.IsInRole(new string[] { "Administrator" }) || User.ID == ownerID;
}