使用枚举来设置元素的可见性?

时间:2013-06-26 12:41:19

标签: c# asp.net enums radio-button

我有16张桌子,属于4个不同的类别,可以说是苹果,橘子,草莓和樱桃。

我正在尝试添加一个带有radiobuttons的过滤器,根据您选择过滤的水果(默认情况下所有水果都是可见的),将可见性设置为真/假。

到目前为止,我有;

我的枚举:

public enum FilterState
    {
        AllFruits,
        Apples,
        Oranges,
        Strawberries,
        Cherries
    }

我的财产

public FilterState FilterChoice
    {
        get { return (FilterState)(ViewState["filterChoice"] ?? FilterState.All); }
        set { ViewState["filterChoice"] = value; }
    }

表格(除了不同的水果外,它们看起来一样):

 <table FilterState="Apples" runat="server" Visible="false">
      ...
 </table

我的eventhandler的开头:

void filterPolicyBtn_Click(object sender, EventArgs e)
    {

        switch (FilterChoice)
        {
            case FilterState.AllFruits:

                //How can i set the states to visible?
                Visible = true;
                break;

            case FilterState.Apples:

                break;

            case FilterState.Oranges:

                break;

            case FilterState.Strawberries:

                break;

            case FilterState.Cherries:

                break;
        }

我知道我可以通过拥有16个不同的ID来解决这个问题,并逐个手动设置它们对单选按钮的可见性。但似乎过度的工作。我也无法更改选项卡的顺序以使用某种包装器,因为它们需要按特定顺序。

我试图以正确的方式解决这个问题?如果是这样,我怎么能完成这个:(任何帮助赞赏!

3 个答案:

答案 0 :(得分:0)

我猜测FilterChoice是保存值的变量。

// Replace apples with the appropriate enumeration
Visible = FilterChoice == FilterState.AllFruits || 
          FilterChoice == FilterState.Apples; 

答案 1 :(得分:0)

我不确定我是否理解正确,但正如我想的那样......

public enum FilterState
{
    Apples = 1,
    Oranges,
    Strawberries,
    Cherries,
    Total
}
public void FilterChoice (int value)
{
    int counter = 1;
    for (int i = 1; i < Total; i++)
        if (value & (counter << i))
            //actions to do if value is set
        else
            //actions if not set...
}

我认为你看起来像这样,我希望它有所帮助。

答案 2 :(得分:0)

好吧所以我发现没有简单的方法可以做到这一点。最后,我必须将ID设置为所有表格,然后使用相同的枚举来设置切换案例中的可见性。

然而,在中途我遇到了带位运算符的枚举标志,它使整个事情变得更加简单。我会在这里为同样的问题提交我的代码:

[Flags]
    public enum FilterState
    {
        Apples = 1,
        Oranges = 1 << 1,
        Strawberries = 1 << 2,
        Cherries = 1 << 3,
        AllFruits = (1 << 4) - 1
    }

除此之外,我还提供了一个帮助类来缩短可见性的设置(不需要,但提到的内容大大缩短了代码):

public static class TestExtension
{
    public static bool HasFlag(this FruitClass.FilterState e1, FruitClass.FilterState e2)
    {
        return ((e1 & e2) == e2);
    }
}

然后所需的“全部”是排序按钮和过滤器功能的事件:

private void Filter(FilterState filter)
    {
        ApplesA.Visible = ApplesB.Visible = ApplesC.Visible = filter.HasFlag(FilterState.Apples);
        OrangesA.Visible = OrangesB.Visible = OrangesC.Visible = OrangesD.Visible = filter.HasFlag(FilterState.Oranges);
        StrawberriesA.Visible = StrawberriesB.Visible = StrawberriesC.Visible = StrawberriesD.Visible = filter.HasFlag(FilterState.Automation);
        CherriesA.Visible = CherriesB.Visible = CherriesC.Visible = filter.HasFlag(FilterState.Cherries);

    }

    private void filterPolicyBtn_Click(object sender, EventArgs e)
    {
        var filter = FilterState.All;

        if (ApplesCheck.Checked) filter = FilterState.Apples;
        if (OrangesCheck.Checked) filter =  FilterState.Oranges;
        if (StrawberriesCheck.Checked) filter = FilterState.Automation;
        if (CherriesCheck.Checked) filter = FilterState.Cherries;

        Filter(filter);

    }

    private void ShowAll_Click(object sender, EventArgs e)
    {
        Filter(FilterState.All);
    }
  • Cammy out